使用 Beaker 缓存和 SQLAlchemy
我正在尝试将烧杯缓存与 SQLAlchemy 一起使用,但我一直收到错误。
这是我的表定义。
class Post(Base):
....
....
user = relation(User, primaryjoin = User.id == id)
tags = relation('Tags', backref = 'posts')
class Tags(Base):
...
...
user = relation(User, primaryjoin = User.id == id)
post = relation(Post, primaryjoin = Post.id == id)
烧杯缓存可与除这些类之外的其他 SQLAlchemy 类一起使用。
当我运行该程序时,我收到以下错误;
DetachedInstanceError: Parent instance <Post at 0x101f90b10> is not bound to a Session; lazy load operation of attribute 'user' cannot proceed.
我在 StackOverFlow 上进行了搜索,并在另一个线程中发现我需要禁用延迟加载,因此我已将该行更改
user = relation(User, primaryjoin = User.id == id)
为,
user = relation(User, primaryjoin = User.id == id, lazy='dynamic')
但这发生在 template(post.user.fullname
); 中的以下错误中:
AttributeError: 'AppenderQuery' object has no attribute 'fullname'
我做错了什么?
I'm trying to use beaker cache with SQLAlchemy but I've been receiving errors.
Here are my table definitions.
class Post(Base):
....
....
user = relation(User, primaryjoin = User.id == id)
tags = relation('Tags', backref = 'posts')
class Tags(Base):
...
...
user = relation(User, primaryjoin = User.id == id)
post = relation(Post, primaryjoin = Post.id == id)
beaker cache works with other SQLAlchemy classes except these ones.
When I run the program, I receive the following error;
DetachedInstanceError: Parent instance <Post at 0x101f90b10> is not bound to a Session; lazy load operation of attribute 'user' cannot proceed.
I've searched on StackOverFlow and have found in another thread that I need to disable lazy loading so I've changed the line
user = relation(User, primaryjoin = User.id == id)
to
user = relation(User, primaryjoin = User.id == id, lazy='dynamic')
but this occurs to following error in template(post.user.fullname
);
AttributeError: 'AppenderQuery' object has no attribute 'fullname'
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您从缓存中获取对象时,应该将它们与会话对象相关联,例如。
when you grab objects from cache, you should associate them with a session object, eg.