I have some problems setting up a dictionary collection in SQLAlchemy Python:
I use a declarative definition of tables. I have a table Itemin a 1: N ratio with a table Record. I established the relationship using the following code:
_Base = declarative_base()
class Record(_Base):
__tablename__ = 'records'
item_id = Column(String(M_ITEM_ID), ForeignKey('items.id'))
id = Column(String(M_RECORD_ID), primary_key=True)
uri = Column(String(M_RECORD_URI))
name = Column(String(M_RECORD_NAME))
class Item(_Base):
__tablename__ = 'items'
id = Column(String(M_ITEM_ID), primary_key=True)
records = relation(Record, collection_class=column_mapped_collection(Record.name), backref='item')
Now I want to work with Itemand Records. Let me create some objects:
i1 = Item(id='id1')
r = Record(id='mujrecord')
And now I want to link these objects with the following code:
i1.records['source_wav'] = r
but Record rdoes not set the attribute name(foreign key). Is there a solution how to automatically provide this? (I know that setting a foreign key during creation Recordworks, but for me this is not very good).
Many thanks