python - SQLAlchemy - MappedCollection problem -
I have some problems to install collection Skyuellemi Python:
I use declarative definition I have the item table of the tables I have: N relationship with the record table. I liaise using the following code:
_Base = declarative_base () class record (_Base): __tablename__ = 'records' item_id = column (string (M_ITEM_ID), foreign key (' items. Id ') ()) id = column (string (M_RECORD_ID), primary_key = true) uri = column (string (M_RECORD_URI)) name = column (string (M_RECORD_NAME)) class items (_Base): __tablename__ =' items' id = Column string (M_aitiaiaidi), Praimri_kij = Trakshn (record, Sngrh_clas = Colm_map_colkshn (Record. name), Bakaf = 'item') I create some objects in the items s and records
i1 = Item (id = 'id1') r = record ( Id = 'mujrecord') And now I want to associate these objects using the following code:
i1.records [' Source_wav '] = r but the record r name attribute is not set (foreign key). Is it any way to ensure this automatically? (I know that record setting up foreign key during the construction work, but it does not look good for me).
Many thanks
You want something like this:
sqlalchemy.orm import class item (_Base) from: [. ..] @ Validates ('Records') DEF is valid _record (self, key, record): Enter Record. The name is nothing, "The record fails verification, it should have a name" Return record
With it, you receive the desired verification:
gt; & Gt; I1 = item (id = 'id1')> & gt; & Gt; & Gt; R = Record (id = 'mjccord')> gt; & Gt; & Gt; I1.records ['source_wav'] = R traceback (most recent call final): [...] AssertionError: If the record verification fails, the name should be; & Gt; & Gt; R.name = 'foo' & gt; & Gt; & Gt; I1.records ['source_wav'] = R & gt; & Gt; & Gt;
Comments
Post a Comment