On 11/29/2015 04:52 PM, Daniel Grace wrote: > I'm working on developing an API that, among other things, allows for > partial updates or insertions to a parent's children. One minor problem > that I've found is that any access to parent.children loads the entire > collection -- when in many cases it's not necessary to do so.
Two ways to have a mutable collection that won't load the whole thing when accessed. You most likely just want to use a "dynamic" relationship. You can append / remove from this collection and it won't load anything, acts just like the dynamic collections found in other ORMs: http://docs.sqlalchemy.org/en/rel_1_0/orm/collections.html?highlight=dynamic#dynamic-relationship-loaders the other technique is to not touch the collection and set up the backref instead, given the canonical user/address pattern, you'd say: address.user = some_user instead of: some_user.addresses.append(user) > > For instance, statements like these -- in a one-to-many relationship (or > potentially some forms of many-to-many relationships, but that's outside > my current use-case.) > > # Add a new child to parent.children. All's this really does is set > child.parent_id, so it doesn't need to know anything about other > children > parent.children.append(new_child) # lists > parent.children.add(new_child) # sets keep in mind that unless you're using "dynamic", these are *real Python lists and sets*. They have no special loading behavior, so when you access object.some_collection, it has no choice but to load. Only if you forego using regular lists/sets/etc. and opt to use a special collection that loads when actually iterated, e.g. the "dynamic" collection, is "give me the collection, but don't load unless we ask" an option here. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.