python - backref lazy='dynamic' - does not support object population - eager loading cannot be applied -
how can planes
bookings
between 2 dates specified. need bookings
filtered down included if they're between 2 dates. tried error: 'plane.bookings' not support object population - eager loading cannot applied.
why error occur , how can fixed?
planes = (db.session.query(plane) .join(booking) .filter(booking.start_date >= date) .options(contains_eager(plane.bookings)))
the models:
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) 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) c in self.__table__.columns}
the error message tells "wrong": lazy='dynamic'
relationship can't eager loaded. dynamic relationships produce new queries, not collections (lists). doesn't make sense pre-load dynamic relationship because entire point of dynamic relationship able construct other queries.
use dynamic relationship filter bookings when accessing them. isn't optimal requires query per plane bookings.
planes = plane.query.join(plane.bookings).filter(booking.start_date >= date) plane in planes: bookings = plane.bookings.filter(booking.start_date >= date)
more likely, wanted non-dynamic relationship. remove lazy='dynamic'
relationship's backref. if pass contains_eager
join has been filtered, resulting collection have filtered items.
plane = db.relationship(lambda: plane, backref='bookings') # ... planes = plane.query.join(plane.bookings ).filter(booking.start_date >= date ).options(db.contains_eager(plane.bookings))
also note there nothing stopping defining 2 relationships same collection, 1 dynamic , 1 not, using whichever needed doing @ time.
Comments
Post a Comment