我收到错误消息:{detachedInstanceerror}父实例未绑定到会话;属性“owner”的延迟加载操作无法继续
我的python代码:

car_obj = my_query_function()  # get a Car object
owner_name = car_obj.owner.name # here generate error!

我的模型:
class Person(EntityClass):
    attributes = ['id', 'name']

    name = sa.Column(sa.String(250))


class Car(EntityClass):
    attributes = ['id', 'brand', 'color', 'purchase_time', 'owner_id']

    brand = sa.Column(sa.String(250))
    color = sa.Column(sa.String(250))
    purchase_time = sa.Column(sa.String(250))
    owner_id = sa.Column(DBKeyType, sa.ForeignKey(Person.__tablename__ + '.id'), nullable=False)
    owner = relationship('Person', cascade='all, delete-orphan', backref=backref('car', cascade='delete'), single_parent=True)

这是否与汽车和用户之间的延迟加载关系设置(多对一关联)有关?我怎样才能修复这段关系?提前谢谢。

最佳答案

我跟踪了这些文档,并通过添加lazy='subquery'

owner = relationship('Person', lazy='subquery', cascade='all, delete-orphan', backref=backref('car', cascade='delete'), single_parent=True)

http://docs.sqlalchemy.org/en/rel_0_9/orm/join_conditions.html

关于python - {DetachedInstanceError}父实例<Car>未绑定(bind)到 session ;属性“所有者”的延迟加载操作无法进行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27701573/

10-12 19:13