[sqlalchemy] Cascading orphan deletes in self-referential table

2010-04-25 Thread Adrian
Hi,

I'm using the following table (shortened, removed unnecessary columns)
to store a menu tree.

class MenuNode(Base):
__tablename__ = 'menu'
id = Column(Integer, primary_key=True, nullable=False,
unique=True)
parent_id = Column(Integer, ForeignKey('menu.id',
onupdate='CASCADE', ondelete='CASCADE'), nullable=True, index=True)
name = Column(String(64), nullable=False)

parent = relationship('MenuNode', remote_side='MenuNode.id',
cascade='all, delete', passive_deletes=True,
backref=backref('children', order_by=position))

Top-level menu nodes have parent_id=NULL.
When deleting a node via session.delete(node) sqlalchemy issues UPDATE
statements to set all children's parent_id fields to NULL instead of
deleting the children.
How can i tell SA that I want to delete any children?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Cascading orphan deletes in self-referential table

2010-04-25 Thread Lance Edgar

On 4/25/2010 6:39 AM, Adrian wrote:

class MenuNode(Base):
 __tablename__ = 'menu'
 id = Column(Integer, primary_key=True, nullable=False,
unique=True)
 parent_id = Column(Integer, ForeignKey('menu.id',
onupdate='CASCADE', ondelete='CASCADE'), nullable=True, index=True)
 name = Column(String(64), nullable=False)

 parent = relationship('MenuNode', remote_side='MenuNode.id',
cascade='all, delete', passive_deletes=True,
backref=backref('children', order_by=position))

Top-level menu nodes have parent_id=NULL.
When deleting a node via session.delete(node) sqlalchemy issues UPDATE
statements to set all children's parent_id fields to NULL instead of
deleting the children.
How can i tell SA that I want to delete any children?


Does specifying cascade='all, delete-orphan' on the parent relationship 
accomplish what you're after?

Lance

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Cascading orphan deletes in self-referential table

2010-04-25 Thread Adrian
 Does specifying cascade='all, delete-orphan' on the parent
 relationship accomplish what you're after?
delete-orphan doesn't work in self-relational relationships (there are
always some nodes without a parent).
However, moving passive_deletes=True into the backref() fixed it.

-- 
Adrian

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.