我试图回答这个问题。我的解决方案应该适用于 SQLAlchemy>=0.8。
实际上这里并没有什么令人惊讶的地方,但是在使用此类模式时必须小心谨慎,因为Sessions 身份映射的状态不会一直反映数据库的状态。
我在relationship 中使用了post_update 开关来打破由此设置产生的循环依赖性。如需更多信息,请查看SQLAlchemy documentation about this。
警告:Session 并不总是反映数据库的状态这一事实可能会导致严重的错误和其他混乱。在这个例子中,我使用expire_all 来显示数据库的真实状态,但这不是一个好的解决方案,因为它会重新加载所有 对象并且所有未flushed 的更改都丢失了。请谨慎使用expire 和expire_all!
首先我们定义模型
#!/usr/bin/env python
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base
engine = sa.create_engine('sqlite:///blah.db')
Base = declarative_base()
Base.bind = engine
class Obj(Base):
__table__ = sa.Table(
'objs', Base.metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('parent_id', sa.Integer, sa.ForeignKey('objs.id')),
sa.Column('deleted', sa.Boolean),
)
# I used the remote() annotation function to make the whole thing more
# explicit and readable.
children = orm.relationship(
'Obj',
primaryjoin=sa.and_(
orm.remote(__table__.c.parent_id) == __table__.c.id,
orm.remote(__table__.c.deleted) == False,
),
backref=orm.backref('parent',
remote_side=[__table__.c.id]),
# This breaks the cyclical dependency which arises from my setup.
# For more information see: http://stackoverflow.com/a/18284518/15274
post_update=True,
)
def __repr__(self):
return "<Obj id=%d children=%d>" % (self.id, len(self.children))
那我们试一试
def main():
session = orm.sessionmaker(bind=engine)
db = session()
Base.metadata.create_all(engine)
p1 = Obj()
db.add(p1)
db.flush()
p2 = Obj()
p2.deleted = True
p1.children.append(p2)
db.flush()
# prints <Obj id=1 children=1>
# This means the object is in the `children` collection, even though
# it is deleted. If you want to prevent this you may want to use
# custom collection classes (not for novices!).
print p1
# We let SQLalchemy forget everything and fetch the state from the DB.
db.expire_all()
p3 = db.query(Obj).first()
# prints <Obj id=1 children=0>
# This indicates that the children which is still linked is not
# loaded into the relationship, which is what we wanted.
print p3
db.rollback()
if __name__ == '__main__':
main()