On Oct 4, 2011, at 8:21 AM, neurino wrote: > I have a mapped class with a one to many relationship to another > class. > > My need is that when a parent class is added to session automatically > it's populated with predefined children too. > > I used to hook to `after-insert` event but now I read it's not > suitable for any Session.add (has it ever been like this or changed > recently?) > > So, while it worked with sqlite, now I get no children at all in > mysql / postgresql > > event.listen(UserStratigraphy, 'after_insert', > user_stratigraphy_after_insert) > > def user_stratigraphy_after_insert(mapper, connection, user_strat): > for layer in DBSession.query(BaseLayer) \ > .filter(BaseLayer.id_str==user_strat.id_str): > user_layer = UserLayer() > user_layer.user_stratigraphy = user_strat > user_layer.layer = layer > DBSession.add(user_layer) > > What should be the best practice to avoid manually add all children > every time I instantiate a new parent?
why not just give Parent a constructor (i.e. __init__) that adds the children it needs ? -- 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.