我有这样的 sqlalchemy 关系(为简单起见进行了修剪):
class Parent(Base):
__tablename__ = 'Parent'
name = Column(String, nullable=False)
def __init__(self, name)
self.name = name
class Child(Base):
__tablename__ = 'Child'
name = Column(String, nullable=False)
parent = relationship(Parent, backref=backref('children')
def __init__(self, name, parent)
self.name = name
self.parent = parent
在我的对象工作时:
parent = Parent("my parent")
db_session.add(parent) # must be done for other reasons not relevant to the issue.
child = Child("my child", parent)
到目前为止,一切都很好。但是在提交之前,当我执行以下操作时,我会获得数据库刷新:
children = parent.children # using the backref causes a flush
可以通过更改我定义反向引用/关系的方式来避免这种情况吗?