Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I pass specific attributes across several objects using SQLAlchemies association_proxy?
    primarykey
    data
    text
    <p>I have two types of objects:</p> <ul> <li>divers <code>ValueObjects</code> that inherit from <code>BaseValueObject</code>, and</li> <li><code>Addendum</code>-objects that carry annotations.</li> </ul> <p><code>Addendum</code> has specific annotations (attributes: <code>name</code>, <code>description</code>, <code>flag</code>) and general annotations (<code>(badgetype, badgevalue)</code>) where the <code>badgevalue</code> is stored in a <code>Badge</code>-object and the <code>badgetype in an</code>AddendumBadgeMap`-object.</p> <p>Now, I would like to access</p> <ul> <li><code>name</code>, <code>description</code>, and <code>flag</code> as attributes from <code>ValueOjects</code> and</li> <li><code>Badges</code> directly from <code>ValueOjects</code> as a dictionary.</li> </ul> <p>Here is an example implementation:</p> <pre><code>import sqlalchemy as sa import sqlalchemy.orm as orm from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base DBSession = scoped_session(sessionmaker()) Base = declarative_base () ###### ValueObjects: ########## class BaseValueObject(Base): __tablename__ = 'value_objects' id = sa.Column(sa.Integer, primary_key=True) vo_type = sa.Column (sa.String, nullable=False) __mapper_args__ = {'polymorphic_on': vo_type} class ObjectOne(BaseValueObject): __tablename__ = 'objects_one' __mapper_args__ = {'polymorphic_identity': 'ObjectOne'} id = sa.Column(sa.ForeignKey('value_objects.id'), primary_key=True) any_attribute = sa.Column(sa.String) class ObjectTwo(BaseValueObject): __tablename__ = 'objects_two' __mapper_args__ = {'polymorphic_identity': 'ObjectTwo'} id = sa.Column(sa.ForeignKey('value_objects.id'), primary_key=True) any_attribute = sa.Column(sa.String) ############################### ##### Addendum and Bagde ########## class Addendum(Base): __tablename__ = 'addenda' __table_args__ = ( sa.UniqueConstraint('name', 'id'), {} ) id = sa.Column (sa.Integer, primary_key=True) name = sa.Column (sa.String, nullable=False, default='') description = sa.Column (sa.String, nullable=False, default='') flag = sa.Column ( sa.Boolean, nullable=False, default=False) value_object_id = sa.Column ( sa.ForeignKey ('value_objects.id'), unique=True) class Badge(Base): __tablename__ = 'badges' id = sa.Column (sa.Integer, primary_key=True) value = sa.Column (sa.String) class AddendumBadgeMap(Base): __tablename__ = 'addendum_badge_maps' __table_args__ = ( sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('badge_id', 'badge_type'), {} ) id = sa.Column('id', sa.Integer) addendum_id = sa.Column(sa.ForeignKey('addenda.id'), nullable=False) badge_id = sa.Column(sa.ForeignKey('badges.id'), nullable=False) badge_type = sa.Column (sa.String) ################################### if __name__ == '__main__': engine = sa.create_engine('sqlite:///:memory:', echo=True) DBSession.configure(bind=engine) Base.metadata.create_all(engine) session = DBSession() o1 = ObjectOne(any_attribute="test1") session.add(o1) session.commit() </code></pre> <p>What do I have to add that I can do the following:</p> <pre><code>o1.name = "my_name" # =&gt; `Addendum` will be attached with `name="my_name"` o1.description = "my_description" # =&gt; `descrpiption` of the attached `Addendum` get's changed o1.flag # =&gt; same behavior (in general: there should be max. one `Addendum` per `ValueObject`) o1.bagdes = {"file_origin": "/home/.."} # =&gt; attach to the `Addendum` a `Badge` with `value="/home/.."` and # `AddendumBagdeMap.badge_type="file_origin"` (if `Addendum` does # not exist for this `ValueObject`: create it first) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload