On Mar 21, 2012, at 5:18 AM, Pau Tallada wrote:

> Hi!
> 
> I have a model with a parent-child relationship on the same entity.
> The "children" collection on a parent instance has events set to intercept 
> adding and removing instances to/from it. This is needed to update an 
> attribute from the parent instance.
> But when I delete an instance which is the child of another one, that parent 
> instance is not notified of its child removal and so the event is not fired.
> 
> I tried working with the cascade rules but I have not been successful.
> 
> Is this behaviour supported?

"delete an instance", meaning,

parent.children.append(child)
session.delete(child)
session.commit()

?


in that case no, "parent.children" is not notified of a collection removal, 
since no collection alteration actually takes place.   The "child" is still 
there in parent.collection until the whole of "parent" is expired, which occurs 
after the commit().  See 
http://docs.sqlalchemy.org/en/latest/orm/session.html#deleting-from-collections 
for detailed discussion on this.

Your two options here are to stick to collection mutations, or do an additional 
before_flush() or after_flush() event that goes through child objects marked 
deleted, finds their parents, and does what is needed.



> 
> Thanks!
> 
> Pau.
> -- 
> ----------------------------------
> Pau Tallada Crespí
> Dep. d'Astrofísica i Cosmologia
> Port d'Informació Científica (PIC)
> Tel: +34 93 586 8233
> ----------------------------------
> 
> 
> 
> -- 
> 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