backref lazy='dynamic' - 不支持对象填充 - 无法应用预加载
backref lazy='dynamic' - does not support object population - eager loading cannot be applied
如何在指定的 2 个日期之间获得 Planes
和 Bookings
。我需要将 Bookings
过滤掉,以便仅在它们介于 2 个日期之间时才包含在内。我试过这个但得到错误:'Plane.bookings' does not support object population - eager loading cannot be applied.
为什么会出现这个错误,如何解决?
planes = (db.session.query(Plane)
.join(Booking)
.filter(Booking.start_date >= date)
.options(contains_eager(Plane.bookings)))
模特:
class Booking(db.Model):
id = db.Column(db.Integer, primary_key=True)
start_date = db.Column(db.DateTime)
end_date = db.Column(db.DateTime)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
person = db.relationship('Person', foreign_keys=[person_id], backref=db.backref('bookings', lazy='dynamic'))
instructor_id = db.Column(db.Integer, db.ForeignKey('person.id'))
instructor = db.relationship('Person', foreign_keys=[instructor_id], backref=db.backref('instructor_bookings', lazy='dynamic'))
plane_id = db.Column(db.Integer, db.ForeignKey('plane.id'))
plane = db.relationship('Plane', backref=db.backref('bookings', lazy='dynamic'))
def __init__(self, start_date, end_date, plane_id, instructor_id, person_id):
self.start_date = start_date
self.end_date = end_date
self.plane_id = plane_id
self.instructor_id = instructor_id
self.person_id = person_id
def __repr__(self):
return '<Booking... %r>'
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Plane(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
description = db.Column(db.String(120))
def __init__(self, name, description):
self.name = name
self.description = description
def __repr__(self):
return '<Plane... %r>'
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
错误消息准确地告诉您什么是 "wrong":无法预加载 lazy='dynamic'
关系。动态关系产生新的查询,而不是集合(列表)。预加载动态关系没有意义,因为动态关系的全部意义在于能够构建其他查询。
访问预订时使用动态关系过滤预订。这不是最佳选择,因为它需要查询每架飞机才能获得预订。
planes = Plane.query.join(Plane.bookings).filter(Booking.start_date >= date)
for plane in planes:
bookings = plane.bookings.filter(Booking.start_date >= date)
更有可能的是,您想要一种非动态关系。只需从关系的反向引用中删除 lazy='dynamic'
。如果您为已过滤的联接传递 contains_eager
,则生成的集合将仅包含过滤后的项目。
plane = db.relationship(lambda: Plane, backref='bookings')
# ...
planes = Plane.query.join(Plane.bookings
).filter(Booking.start_date >= date
).options(db.contains_eager(Plane.bookings))
另请注意,没有什么可以阻止您为同一个集合定义两种关系,一种是动态的,另一种不是,使用您当时正在做的事情需要的任何一种。
如何在指定的 2 个日期之间获得 Planes
和 Bookings
。我需要将 Bookings
过滤掉,以便仅在它们介于 2 个日期之间时才包含在内。我试过这个但得到错误:'Plane.bookings' does not support object population - eager loading cannot be applied.
为什么会出现这个错误,如何解决?
planes = (db.session.query(Plane)
.join(Booking)
.filter(Booking.start_date >= date)
.options(contains_eager(Plane.bookings)))
模特:
class Booking(db.Model):
id = db.Column(db.Integer, primary_key=True)
start_date = db.Column(db.DateTime)
end_date = db.Column(db.DateTime)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
person = db.relationship('Person', foreign_keys=[person_id], backref=db.backref('bookings', lazy='dynamic'))
instructor_id = db.Column(db.Integer, db.ForeignKey('person.id'))
instructor = db.relationship('Person', foreign_keys=[instructor_id], backref=db.backref('instructor_bookings', lazy='dynamic'))
plane_id = db.Column(db.Integer, db.ForeignKey('plane.id'))
plane = db.relationship('Plane', backref=db.backref('bookings', lazy='dynamic'))
def __init__(self, start_date, end_date, plane_id, instructor_id, person_id):
self.start_date = start_date
self.end_date = end_date
self.plane_id = plane_id
self.instructor_id = instructor_id
self.person_id = person_id
def __repr__(self):
return '<Booking... %r>'
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Plane(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
description = db.Column(db.String(120))
def __init__(self, name, description):
self.name = name
self.description = description
def __repr__(self):
return '<Plane... %r>'
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
错误消息准确地告诉您什么是 "wrong":无法预加载 lazy='dynamic'
关系。动态关系产生新的查询,而不是集合(列表)。预加载动态关系没有意义,因为动态关系的全部意义在于能够构建其他查询。
访问预订时使用动态关系过滤预订。这不是最佳选择,因为它需要查询每架飞机才能获得预订。
planes = Plane.query.join(Plane.bookings).filter(Booking.start_date >= date)
for plane in planes:
bookings = plane.bookings.filter(Booking.start_date >= date)
更有可能的是,您想要一种非动态关系。只需从关系的反向引用中删除 lazy='dynamic'
。如果您为已过滤的联接传递 contains_eager
,则生成的集合将仅包含过滤后的项目。
plane = db.relationship(lambda: Plane, backref='bookings')
# ...
planes = Plane.query.join(Plane.bookings
).filter(Booking.start_date >= date
).options(db.contains_eager(Plane.bookings))
另请注意,没有什么可以阻止您为同一个集合定义两种关系,一种是动态的,另一种不是,使用您当时正在做的事情需要的任何一种。