SQLAlchemy 1.2 and up support multi table deletes for some dialects (at the time of writing Postgresql, MySQL and Microsoft SQL Server):
In [18]: a = table('a', column('x'))
In [19]: b = table('b', column('x'))
In [20]: c = table('c', column('x'), column('y'))
In [21]: a.delete().\
...: where(a.c.x == b.c.x).\
...: where(b.c.x == c.c.x).\
...: where(c.c.y == 1)
Out[21]: <sqlalchemy.sql.dml.Delete object at 0x7f3577d89160>
In [22]: print(_.compile(dialect=mysql.dialect()))
DELETE FROM a USING a, b, c WHERE a.x = b.x AND b.x = c.x AND c.y = %s
and the same using a Session
and Declarative:
In [2]: class Foo(Base):
...: __tablename__ = 'foo'
...: id = Column(Integer, primary_key=True)
In [3]: class Bar(Base):
...: __tablename__ = 'bar'
...: id = Column(Integer, primary_key=True)
...: foo_id = Column(ForeignKey(Foo.id))
...:
In [4]: class Baz(Base):
...: __tablename__ = 'baz'
...: id = Column(Integer, primary_key=True)
...: bar_id = Column(ForeignKey(Bar.id))
...: val = Column(Integer)
...:
In [5]: session.query(Foo).\
...: filter(Foo.id == Bar.foo_id,
...: Bar.id == Baz.bar_id,
...: Baz.val == 1).\
...: delete(synchronize_session=False)
...:
which would emit
DELETE FROM foo USING foo, bar, baz
WHERE foo.id = bar.foo_id AND bar.id = baz.bar_id AND baz.val = %(val_1)s