you want to link "relationships" and "source" using a backref, so that the 
reference is updated both directions without doing a database round trip.

See 
http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html?highlight=backrefs#linking-relationships-with-backref
 .



On Apr 17, 2014, at 1:04 PM, Mats Nordgren <mats.nordg...@gmail.com> wrote:

> class Person(Entity):
>     '''A person.'''
>     
>     __tablename__ = 'people'
>     
>     id = Column(Integer, ForeignKey('entities.id'), primary_key=True)
>     
>     first_name = Column(String(50))
>     middle_name = Column(String(50))
>     last_name = Column(String(50))
>     
>     date_of_birth = Column(Date)
>     date_of_death = Column(Date)
>     
>     relationships = relationship('Relationship', 
> foreign_keys='relationships.c.source_id')
>     
>     __mapper_args__ = {
>         'polymorphic_identity': 'person'}
> 
> Missed the mapper args part of person.
> 
> 
> On Thursday, April 17, 2014 10:02:54 AM UTC-7, Mats Nordgren wrote:
> class Entity(Base):
>     '''A base for entities.'''
>     
>     __tablename__ = 'entities'
>     
>     id = Column(Integer, Sequence('entity_id_seq'), primary_key=True)
>     type = Column(String(50))
>     
>     tax_id = Column(String(20))
>     
>     memberships = relationship('Membership')
>     
>     __mapper_args__ = {
>         'polymorphic_identity': 'entity',
>         'polymorphic_on': type}
> 
> class Person(Entity):
>     '''A person.'''
>     
>     __tablename__ = 'people'
>     
>     id = Column(Integer, ForeignKey('entities.id'), primary_key=True)
>     
>     first_name = Column(String(50))
>     middle_name = Column(String(50))
>     last_name = Column(String(50))
>     
>     date_of_birth = Column(Date)
>     date_of_death = Column(Date)
>     
>     relationships = relationship('Relationship', 
> foreign_keys='relationships.c.source_id')
> 
> class Relationship(Base):
>     '''A relationship.'''
>     
>     __tablename__ = 'relationships'
>     
>     source_id = Column(Integer, ForeignKey('people.id'), primary_key=True)
>     target_id = Column(Integer, ForeignKey('people.id'), primary_key=True)
>     relationship_type_id = Column(Integer, 
> ForeignKey('relationship_types.id'), primary_key=True)
>     
>     from_date = Column(Date)
>     thru_date = Column(Date)
>     
>     notes = Column(Text)
>     
>     source = relationship('Person', foreign_keys=source_id)
>     target = relationship('Person', foreign_keys=target_id)
>     relationship_type = relationship('RelationshipType', 
> foreign_keys=relationship_type_id)
> 
> class RelationshipType(Base):
>     '''A relationship type.'''
>     
>     __tablename__ = 'relationship_types'
>     
>     id = Column(Integer, Sequence('relationship_type_id_seq'), 
> primary_key=True)
>     
>     description = Column(String(50))
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to