what youre looking for is similar to what the SelectResults plugin will give you when you ask for count(). however, the lazy-loading relationships off of a parent object are hardwired to load the instances in when first referenced; since the list of child objects attached to a parent is a mutable list which is also history-tracked, the transformative behavior of SelectResults is not really appropriate there. it might not be impossible, but would be extremely involved to change this behavior right now (i.e., you lazy load the attribute, you get a SelectResults instance, which magically becomes the history-tracked/mutable list as soon as you try to write to it). other options include maybe an option "lazy='readonly'" or maybe some alternate calling schemes, such as Parent.children.selectresults(someparent)...which is a little ugly.
since youre looking for "parent.children.<call methods on 'children' where the data hasnt actually been loaded>", you can also just add your own properties to your classes that just call whatever query you want to get a similar effect. There are also issues with wiring up __len__() to call COUNT, particularly with the SeletResults plugin; read the comments in http://www.sqlalchemy.org/trac/ticket/180 for a description. On Aug 3, 2006, at 1:04 AM, Randall Smith wrote: > I ran into a not so unusual case today where I need a count of > children > for each parent in a one to many relationship. I first thought of > what > I would do with SQL. I would join the tables and use a COUNT > aggregate. > Then I thought about how I might approach it with SQLAlchemy. There > is the select approach that is almost identical to the SQL approach > and > then there is the ORM approach. I don't have many parent records, > so I > don't mind selecting them all, but I certainly don't want to select > all > of the child records. > > parents = session.query(Parent).select() > counts = [len(parent.children) for parent in parents] > > The problem with this is that len(parent.children) selects all of the > child records (I think). It would be nice if len(parent.children) > triggered a COUNT instead. Or maybe children could have a count > method > so it would look like this: > > parents = session.query(Parent).select() > counts = [parent.children.count() for parent in parents] > > Does a simple and efficient ORM approach already exist? If not, > what do > you think of these two approaches? > > Randall > > > ---------------------------------------------------------------------- > --- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to > share your > opinions on IT & business topics through brief surveys -- and earn > cash > http://www.techsay.com/default.php? > page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Sqlalchemy-users mailing list > Sqlalchemy-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users