title: SQLAlchemy: cascade delete
body: <p>I must be missing something trivial with SQLAlchemy's cascade options because I cannot get a simple cascade delete to operate correctly -- if a parent element is a deleted, the children persist, with <code>null</code> foreign keys. </p>
  
  <p>I've put a concise test case here:</p>
  
  <pre><code>from sqlalchemy import Column, Integer, ForeignKey
  from sqlalchemy.orm import relationship
  
  from sqlalchemy import create_engine
  from sqlalchemy.orm import sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  
  Base = declarative_base()
  
  class Parent(Base):
      __tablename__ = "parent"
      id = Column(Integer, primary_key = True)
  
  class Child(Base):
      __tablename__ = "child"
      id = Column(Integer, primary_key = True)
      parentid = Column(Integer, ForeignKey(Parent.id))
      parent = relationship(Parent, cascade = "all,delete", backref = "children")
  
  engine = create_engine("sqlite:///:memory:")
  Base.metadata.create_all(engine)
  Session = sessionmaker(bind=engine)
  
  session = Session()
  
  parent = Parent()
  parent.children.append(Child())
  parent.children.append(Child())
  parent.children.append(Child())
  
  session.add(parent)
  session.commit()
  
  print "Before delete, children = {0}".format(session.query(Child).count())
  print "Before delete, parent = {0}".format(session.query(Parent).count())
  
  session.delete(parent)
  session.commit()
  
  print "After delete, children = {0}".format(session.query(Child).count())
  print "After delete parent = {0}".format(session.query(Parent).count())
  
  session.close()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Before delete, children = 3
  Before delete, parent = 1
  After delete, children = 3
  After delete parent = 0
  </code></pre>
  
  <p>There is a simple, one-to-many relationship between Parent and Child. The script creates a parent, adds 3 children, then commits. Next, it deletes the parent, but the children persist. Why? How do I make the children cascade delete?</p>