title: One-to-one delete for SQLAlchemy classes with inheritance
body: <p>I have a class inheritance scheme as layed out in <a href="http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance" rel="nofollow">http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance</a></p>
  
  <pre><code>from sqlalchemy import Column, Integer, String, ForeignKey
  from sqlalchemy.ext.declarative import declarative_base
  
  
  Base = declarative_base()
  
  
  class Parent(Base):
      __tablename__ = 'parent'
  
      id = Column(Integer, primary_key=True)
      type = Column(String)
  
      __mapper_args__ = {'polymorphic_on': type}
  
  
  class Child(Parent):
      __tablename__ = 'child'
  
      id = Column(Integer, ForeignKey('parent.id'), primary_key=True)
  
      __mapper_args__ = {'polymorphic_identity': 'child'}
  </code></pre>
  
  <p>Having created an instance of <code>Child</code>, I'd like to be able to delete either the <code>Child</code> or the <code>Parent</code> reference without the other hanging around, but that's not what happens. When I fire up IPython...</p>
  
  <pre><code>In [1]: from stackoverflow.question import Parent, Child
  
  In [2]: from sqlalchemy import create_engine
  
  In [3]: from sqlalchemy.orm import sessionmaker
  
  In [4]: session = sessionmaker(bind=create_engine(...), autocommit=True)()
  
  In [5]: with session.begin():
     ...:     session.add(Child())
     ...:     
  
  In [6]: session.query(Parent).all()
  Out[6]: [&lt;stackoverflow.question.Child at 0x7fe408030240&gt;]
  
  In [7]: session.query(Parent).delete()
  ---------------------------------------------------------------------------
  IntegrityError                            Traceback (most recent call last)
  /.../lib/python3.4/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
     1062                         parameters,
  -&gt; 1063                         context)
     1064         except Exception as e:
  
  ...
  --&gt; Big traceback here...
  ...
  
  IntegrityError: (IntegrityError) update or delete on table "parent" violates foreign key constraint "child_id_fkey" on table "child"
  DETAIL:  Key (id)=(1) is still referenced from table "child".
   'DELETE FROM parent' {}
  
  In [8]: session.query(Child).delete()
  Out[8]: 1
  
  In [9]: session.query(Parent).all()
  Out[9]: [&lt;stackoverflow.question.Child at 0x7fe408030240&gt;]
  </code></pre>
  
  <p>Deleting the <code>Parent</code> instance doesn't work, and deleting the <code>Child</code> instance leaves the <code>Parent</code> instance alive and well...</p>
  
  <p>After experimenting a bit, setting the keyword argument <code>ondelete='CASCADE'</code> in the <code>ForeignKey</code> in <code>Child</code> allows me to delete the <code>Parent</code> (which cascades to the <code>Child</code>) but the reverse doesn't happen. Is there any way to configure these two classes so that deleting the <code>Child</code> also deletes the <code>Parent</code>?</p>