On Fri, May 24, 2019, at 12:48 PM, Jonathan Vanasco wrote: > 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.
So, this is absolutely how this was intended. However, for years, and years, and years, people constantly ask for their object-bound relationships to be filtered, hence contains_eager() was created to make this possible. I don't think I have *ever* used contains_eager() to produce a filtered relationship, for the reasons you mention; the collection is defined as *all* records and if you start messing with that, you don't know what you have, because the ORM uses an identity map. But boy, do people want it to be filtered. I don't know what they're doing, and perhaps if everyone that uses filtered relationships eventually realizes it's a bad idea, if there were some way to describe that. I mean, see if you can convince the person who posted issue https://github.com/sqlalchemy/sqlalchemy/issues/4472 <https://github.com/sqlalchemy/sqlalchemy/issues/4472#issuecomment-458995069> they don't need to do this. > > 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. yup > > > 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. this is kind of the documented purpose of contains_eager(), to filter the join condition, so if you are using it for a collection, it's the exception rather than the norm that you'd be getting back primary objects with their complete collections associated. > > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/d89515ae-ea7b-44ca-b8c7-e1782a0801e2%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/d89515ae-ea7b-44ca-b8c7-e1782a0801e2%40googlegroups.com?utm_medium=email&utm_source=footer>. > 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. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/3bb60d12-79c2-46cd-82fa-5fc66005bfad%40www.fastmail.com. For more options, visit https://groups.google.com/d/optout.