you'd probably want to put the criterion in the ON clause:
session
.query
(User).add_entity(Address).select_from(users.outerjoin(addresses,
and_(Address.type=='home', Address.user_id=User.id)))
alternatively you can shove the actual subquery in there in a few
ways, one of them is like this:
sel = addresses.select().where(Address.type=='home')
session.query(User).add_entity(Address).outerjoin(('addresses',
sel))
or otherwise spell out the join to the subquery using select_from()
again.
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/2r-CzHOnGx4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CA%2BRjkXFmGu8vUR4%3D88vP%2BZ%3Dvi-bgOjPwCgx6r912XX_MF6-VOQ%40mail.gmail.com.
>>Was aiming for DRY( don't repeat yourself) for the relation : once i declare the relation, i should not have to join to it again, to filter it.
To be clearer,The relationship could be loaded lazy or selectin, not necessarily a join.It should load as defined, but with the filter applied.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CALbcBG%2B8WZoFGF_8T7OnakiQm6F83zUY%2BwGFvURjjFmCzX-XQA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/b20d6045-5a6d-4bbf-ab50-2e203a0d6950%40www.fastmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexefniB1%2B3_DWzyjrm8B%2Biwcb64PJZVzft8oTH%3DjLy8sDg%40mail.gmail.com.
About DRY :If i define a join condition in a relationship, but still have to define it again in the queries, it seems to go against DRY.If the condition changes, i would have to change it everywhere. I suppose we could have method like joinAddress(query) to do it in a single place.Consider getting only Mumbai city addresses of all users.qry_users = session.query(User).outerjoin(User.addresses, Address.city=='Mumbai').options(contains_eager(User.addresses)).all()Currently, the above is not allowed, i.e. specify additional condition for a relation in join.So we have to user Address entity and add the join condition again.qry_users = session.query(User).outerjoin(Address, (Address.user_id == User.id) & (Address.city=='Mumbai')).options(contains_eager(User.addresses)).all()If in this case, it were allowed to add the condition to the relation, it would be nice.
But it still forces me to use a join, whereas my relation could be set to load differently.I know query api is pretty complex and flexible, and i am new to sqlalchemy, so i probably don't understand many things well.I was thinking that if we had a options(filterrelated(User.addresses, Address.city=='Mumbai')), it would fetch Users,and for each User, it would set this filter condition in User.addresses.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CALbcBG%2B5XcftoxL5zOGUZFf2Vntso_oMX58rj6HXjgY4LQcQKw%40mail.gmail.com.
I agree with Simon, and I think I'm very much -1 on the usage of contains_eager above (and I think that ticket you linked to, Mike). I find that sort of stuff causes a lot of bugs in the long run.I am looking at it from this perspective, which is the same as Simon's but some stronger language...The relationship `User.addresses` is specified as the logical relationship of a User to *all* Addresses.If the addresses are filtered to only "home" or a particular city, that collection does not represent **all** Addresses and should not be mapped to the `.addresses` relationship.
By allowing `contains_eager` on that filtered view, it is very possible that another section of code will be utilizing the object and not knowing that .addresses is filtered.
The filtered view is Addresses, but not User.addresses. I've probably wanted to do this myself in the past, but experience has really taught me that this should not be allowed.I'd be +1 on a feature that prevents `contains_eager` to be invoked on a relationship if it is filtered/joined differently - though that would be immensely hard to detect on complex joins and likely not worth the effort.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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+...@googlegroups.com.To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/d89515ae-ea7b-44ca-b8c7-e1782a0801e2%40googlegroups.com.