Thanks, this worked in UserStratigraphy class:
@validates('id_str')
def add_user_layers(self, key, id_str):
for layer in DBSession.query(BaseLayer) \
.filter(BaseLayer.id_str==id_str):
user_layer = UserLayer()
user_layer.user_stratigraphy = self
user_layer.layer = layer
return id_str
> and yes, it has always been a restriction that the Session cannot be
reliably manipulated from inside the flush procedure.
I had no doubt, honeslty, I was coming from an old project using
MapperExtension.
Now, being deprecated, I just tried `event.listen` as a replacement.
Thanks again for your support
neurino
On Tue, Oct 4, 2011 at 4:06 PM, Michael Bayer <[email protected]>wrote:
>
> On Oct 4, 2011, at 9:37 AM, neurino wrote:
>
> > 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...
>
> manual is never required for something that follows a repeatable rule -
> it's just mapper events are the worst place to do things, and yes, it has
> always been a restriction that the Session cannot be reliably manipulated
> from inside the flush procedure. You can add event listeners to the
> relationship in question using @validates, which then apply the
> corresponding extra objects. I'd still keep this at the object level. As
> a fallback, you'd hit it with the before_flush event. An eventual feature
> for "before_flush" is to break it into sub-events that can be limited to
> certain classes and statuses, but this is very easy to roll manually doing a
> simple "[obj for obj in session.dirty if isinstance(obj, MyClass)]" for now.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.