> -----Original Message----- > From: sqlalchemy@googlegroups.com > [mailto:sqlalch...@googlegroups.com] On Behalf Of Marcin Krol > Sent: 28 May 2009 10:09 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] This join does not fill in the collection > > > Hello everyone, > > session.query(Reservation, > Host).join(Reservation.hosts).filter(Reservation ...).all() > > Reservation.hosts is not filled in, when I access .hosts > collection in > individual Reservations, SQLA issues queries to fill in the > collection > one Reservation by one. > > Again, Reservation and Hosts are many to many relation. > > I certainly can group the Host objects to particular > Reservations myself > later. > > But... Is there a way to make SQLA do it itself at query > time? I haven't > found anything in the docs that would suggest that.. > > Regards, > mk >
If your query had also filtered by some of the host columns, the result set wouldn't contain all the hosts for each Reservation returned. For this reason, SQLAlchemy doesn't assume that just because the host columns are available they represent the entire Reservation.hosts collection. If you want Reservation.hosts to be filled in by the query, you want "eager loading". This can be configured for all queries when you define the relation (by setting lazy=False), or on a query-by-query basis by adding an 'eagerload' option to the query. Both of these methods will add an extra join to the hosts table in your query (separate from any join that you explicitly ask for) If you've added an explicit join and you know that the result set already contains all the information you need, you can use the contains_eager option to indicate that the the relation should be filled in from the information in the result set. Documentation for most of this is at http://www.sqlalchemy.org/docs/05/mappers.html#configuring-loader-strate gies-lazy-loading-eager-loading Hope that helps, Simon --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---