On Fri, Feb 23, 2018 at 10:29 PM,  <jens.troe...@gmail.com> wrote:
> Hi,
>
> I’d like to follow up on the Backref Arguments example from the
> documentation.
>
> First, to make sure I understand this correctly, are these two lines
> equivalent? I would assume so, considering this comment: “By default, this
> value [primaryjoin] is computed based on the foreign key relationships of
> the parent and child tables (or association table).”
>
> addresses = relationship("Address",
>                          backref="user")
>
> # Is like saying…
>
> addresses = relationship("Address",
>                          primaryjoin="User.id==Address.user_id",
>                          backref="user")

they are equivalent assuming that's the natural primaryjoin condition
that would be derived from the two mapped tables, sure.

>
> Now for the backref with an argument. In the example, the email’s local part
> `tony` is hardcoded. Is there a way to parameterize that local part
> elegantly (i.e. pass arguments to that relationship), or is the following
> function the recommended way to go:
>
> def user_addresses(self, name):
>     return [a for a in self.addresses if a.email.startswith(name)]

there's a recipe at
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/GlobalFilter.
     If you have a very small number of addresses, then the in-Python
filtering option is fine.   other options are dynamic relationship
(http://docs.sqlalchemy.org/en/latest/orm/collections.html#dynamic-relationship)
and using object_session
(http://docs.sqlalchemy.org/en/latest/orm/join_conditions.html#building-query-enabled-properties)




>
> Thanks!
> Jens
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to