How To Use Joinedload/contains_eager For Query-enabled Relationships (lazy='dynamic' Option) In Sqlalchemy
I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer, primary_key=True) name = Column(String, nullable=False, unique=True) c
Solution 1:
The problem is that the property on User
for posts is a dynamic relationship; It's supposed to return a Query
object. There's no way for the property to know, or safely communicate, that this time, all of the related items are already loaded.
The simple workaround will be to have two properties, one that uses the normal lazy loading behavior (that you can set to eager load for specific queries where it makes sense), and another that always returns a dynamic relationship.
class User(Base):
id =Column(Integer, primary_key=True)
name =Column(String, nullable=False, unique=True)
created_at = Colmn(DateTime, nullable=False, default=func.now())
class Post(Base):
id =Column(Integer, primary_key=True)
user_id =Column(Integer, ForeignKey(User.id), nullable=False)
user= relationship(User, backref=backref('posts'))
title =Column(String, nullable=False)
body =Column(Text, nullable=False)
created_at = Colmn(DateTime, nullable=False, default=func.now())
User.post_query = relationship(Post, lazy="dynamic")
Post a Comment for "How To Use Joinedload/contains_eager For Query-enabled Relationships (lazy='dynamic' Option) In Sqlalchemy"