Re: [sqlalchemy] Re: Foreign Keys and 'backref'
On Thu, 31 Jul 2014, Jonathan Vanasco wrote: backref indicates the string name of a property to be placed on the related mapper’s class that will handle this relationship in the other direction. The other property will be created automatically when the mappers are configured. Can also be passed as a backref() object to control the configuration of the new relationship. Using your examples side-by-side: user = relationship(User, backref=backref('addresses', order_by=id)) addresses = relationship(Address, backref=user) Jonathan, So I can specify the relationship from either class/table. I'll read the docs again to make sure I fully understand which form to use when. Thank you, 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.
Re: [sqlalchemy] Re: Foreign Keys and 'backref'
On Thu, 31 Jul 2014, Jonathan Vanasco wrote: The relevant docs say this ( http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#sqlalchemy.orm.relationship.params.backref ) Re-reading this section of the docs I'm seeing what I missed the first time through. Thanks again, Jonathan, 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.
Re: [sqlalchemy] Re: Foreign Keys and 'backref'
yes. `backref` just lets you consolidate defining a relationship in a single place. -- `relationship` lets you specify the relationship from A to B `backref` is a kwarg to `relationship` that lets you specify the inverse ( B to A ) class TableA(): b = relationship(TableB, backref=a) class TableB(): pass is the same thing as saying: class TableA(): b = relationship(TableB) class TableB(): a = relationship(TableA) `backref` can either be a `String` OR a `backref()` constructor -- which lets you customized the join with the same conditions you can elect in the `relationship` constructor class Foo(): bar = relationship(Bar, lots_of_keywords_here=True, backref=backref(the_same_keywords_work_here=True) ) -- 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.
Re: [sqlalchemy] Re: Foreign Keys and 'backref'
On Thu, 31 Jul 2014, Jonathan Vanasco wrote: yes. `backref` just lets you consolidate defining a relationship in a single place. `relationship` lets you specify the relationship from A to B `backref` is a kwarg to `relationship` that lets you specify the inverse ( B to A ) class TableA(): b = relationship(TableB, backref=a) class TableB(): pass is the same thing as saying: class TableA(): b = relationship(TableB) class TableB(): a = relationship(TableA) `backref` can either be a `String` OR a `backref()` constructor -- which lets you customized the join with the same conditions you can elect in the `relationship` constructor class Foo(): bar = relationship(Bar, lots_of_keywords_here=True, backref=backref(the_same_keywords_work_here=True) ) Jonathan, That makes everything clear. My original confusion arose because I'm used to specifying many children to one parent with the postgres syntax REFERENCES in the children's table; that's the only relationships I've used (other than many-to-many in linking tables). The abundance of options in SQLAlchemy threw me. Much appreciated, 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.