On Thu, 21 May 2015, Mike Bayer wrote:

I think the best use case is to put it on both, using back_populates.

  Validation check: am I correctly using relationship() in the following set
of three tables? (N.B. Other columns removed for clarity and space saving.)

class Agencies(Base):
    __tablename__ = 'agencies'
    name = Column(Unicode(48), Sequence('name_seq'), primary_key = True)
    ...
    child1 = relationship('AgencyUnits', back_populates = 'agency', cascade = 
'all, delete, delete-orphan')
    child2 = relationship('Permits', back_populates = 'agency', cascade = 'all, 
delete, delete-orphan')
    child3 = relationship('Inspect', back_populates = 'agency', cascade = 'all, 
delete, delete-orphan')

class AgencyUnits(Base):
    __tablename__ = 'agencyUnits'
    name = Column(Unicode(48), Sequence('name_seq'), primary_key = True)
    parent_name = Column(Unicode(48), nullable = False, ForeignKey(\
        'agencies.name'))
    parent_id = relationship("Agency", back_populates = 'units')
    ...
    child = relationship("AgencyContacts", back_populates = 'units', cascade = 
'all, delete, delete-orphan')

class AgencyContacts(Base):
    __tablename__ = 'agencyContacts'
    contact_id = Column(Integer, Sequence('contact_id_seq'), primary_key=True)
    ...
    unit_id = Column(Unicode(48), ForeignKey('agencyUnits.name'))
    parent = relationship("AgencyUnits", back_populates = 'contacts')

Rich


Reply via email to