Re: [sqlalchemy] relationsip(): In Parent or Child Class?
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
Re: [sqlalchemy] relationsip(): In Parent or Child Class?
On Thu, 21 May 2015, Mike Bayer wrote: I think the best use case is to put it on both, using back_populates. This is the focus of current documentation: http://docs.sqlalchemy.org/en/rel_1_0/orm/backref.html It's more verbose but I think it's clearer and functionally this is what backref does behind the scenes in any case. Mike, OK. I'm carefully working through the ORM tutorial and cleaning code as I go. Thanks, Rich
Re: [sqlalchemy] relationsip(): In Parent or Child Class?
On 5/21/15 4:32 PM, Rich Shepard wrote: There are a number of many-to-one table/class relationships in the application. In the many class I use ForeignKey() to relate that column to the appropriate 'one' class and column. Reading the ORM tutorial tells me that the relationship() function can be in either table. I can specify the backref from the many class to the one class or define it as a child in the one class with the backref to the many class. Is one placement preferred over the other? If not, what criteria are used to determine where the relationship() function should be placed? I think the best use case is to put it on both, using back_populates. This is the focus of current documentation: http://docs.sqlalchemy.org/en/rel_1_0/orm/backref.html It's more verbose but I think it's clearer and functionally this is what backref does behind the scenes in any case. Rich -- 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.