Added support for backrefs using sqlalchemy.orm.backref
which returns a tuple.
This commit is contained in:
@@ -227,6 +227,8 @@ def auto_delete_orphans(attr):
|
|||||||
'The relationship argument given for auto_delete_orphans needs to '
|
'The relationship argument given for auto_delete_orphans needs to '
|
||||||
'have a backref relationship set.'
|
'have a backref relationship set.'
|
||||||
)
|
)
|
||||||
|
if isinstance(backref, tuple):
|
||||||
|
backref = backref[0]
|
||||||
|
|
||||||
@sa.event.listens_for(sa.orm.Session, 'after_flush')
|
@sa.event.listens_for(sa.orm.Session, 'after_flush')
|
||||||
def delete_orphan_listener(session, ctx):
|
def delete_orphan_listener(session, ctx):
|
||||||
@@ -249,7 +251,7 @@ def auto_delete_orphans(attr):
|
|||||||
(
|
(
|
||||||
session.query(target_class)
|
session.query(target_class)
|
||||||
.filter(
|
.filter(
|
||||||
~getattr(target_class, attr.property.backref).any()
|
~getattr(target_class, backref).any()
|
||||||
)
|
)
|
||||||
.delete(synchronize_session=False)
|
.delete(synchronize_session=False)
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user