On Jan 9, 2008, at 3:19 PM, Michael Bayer wrote:

> On Jan 4, 2008, at 9:07 AM, Laurent Houdard wrote:
>
>>
>> I have nodes with a self-referential parent-child one-to-many
>> relation, and a many-to-many relation between nodes and keywords.
>>
>> I would like to manage orphan keywords. It doesn't work with
>> cascade=delete-orphan which is not adapted to many-to-many relations.
>
> delete-orphan works with many-to-many relations.

well let me correct myself.  its not going to work properly if you  
actually have many parents related to a single child node, since it  
would have to load in the full list of parents to detect the "orphan"  
condition correctly.

So if I understand what youre looking to do here,  youd like a keyword  
to be deleted when the last parent is removed ?   cleanest way to do  
that would most likely be via triggers or a cleanup routine that runs  
periodically.

>> I could do it manually, but I can't figure how because nodes can be
>> deleted automatically when deleting a parent node (cascade=delete in
>> parent-child relation). I looked at MapperExtension and
>> SessionExtension without success...
>>
>>

if youd like to check for this condition after each flush you can do  
it with SessionExtension, using after_flush().   it could be done in a  
single DELETE statement, i.e.

delete from keywords where not exists(select 1 from  
keyword_associations where keyword_id=keywords.id)

for any approach, a strategy like the delete statement above can be  
used.

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

Reply via email to