Hi Michael,

the reason is Parent does not know its children until another relationship
is set (`id_str` in the code above).

Consider this form validation part:

if form.validate():
    user_strat = form.bind(UserStratigraphy())

I cannot bind prior creating an instance and this is quite a common
situation.

I can always go with manual adds of course but...

==================

To give more details I have a UserStratigraphy => UserLayer, containing user
data, that lays over a fixed Stratigraphy => Layer structure.

When a user creates a new UserStratigraphy and tells me which Stratigraphy
it belongs I wish I can create also needed UserLayers accordingly to save
relative data.

Little mind map: http://postimage.org/image/286v46m10/


Thanks for your support
neurino


On Tue, Oct 4, 2011 at 3:17 PM, Michael Bayer <mike...@zzzcomputing.com>wrote:

>
> 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.
>
>

-- 
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.

Reply via email to