How can I cascade delete from a many to one only when deleting the many 
means this one has no other children?

Imagine the following mapping:

    
class Container(Base):
    __tablename__ = 'container'
    pk = sa.Column(sa.Integer, primary_key=True)
    entries = relationship('Entry', backref='container', lazy='dynamic', 
cascade="all, delete, delete-orphan")

class Entry(Base):
    __tablename__ = 'entry'
    pk = sa.Column(sa.Integer, primary_key=True)
    container_pk = sa.Column(sa.Integer, sa.ForeignKey('container.pk'),
    tag_pk = sa.Column(sa.Integer, sa.ForeignKey('tag.pk'))

class Tag(Base):
    __tablename__ = 'tag'
    pk = sa.Column(sa.Integer, primary_key=True)
    description = sa.Column(sa.String, nullable=False)
    entries = relationship("Entry", backref='tag')

A Container has zero or more Entry objects. An Entry may be grouped with 
other Entry objects with a Tag. A Tag is never shared with Entry instances 
from more than one Container.

If I delete a Container instance, all of its Entry instances get deleted 
via cascade. What I'd also like to happen is to cascade the delete to all 
of the Tags as well. Most of the documentation I've seen for cascading 
deletes talks about deleting parents cascading to their children. In this 
case, it seems like I want to delete the parent (Tag) if I'm the last child 
(Entry). I'd like to avoid putting the Container pk on the Tag since it's 
already on the Entry and that seems like it's not properly normalized.

My example seems close to the "Many-to-many orphan deletion" given in 
sqlalchemy-utils, except it's many-to-one.

http://sqlalchemy-utils.readthedocs.org/en/latest/listeners.html#many-to-many-orphan-deletion

Any ideas?




 




-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to