使用 Beaker 缓存和 SQLAlchemy

发布于 2024-10-17 19:26:20 字数 1019 浏览 8 评论 0原文

我正在尝试将烧杯缓存与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-10-24 19:26:20

当您从缓存中获取对象时,应该将它们与会话对象相关联,例如。

obj_from_cache = get_from_cache(key)
session.merge(obj_from_cache)

when you grab objects from cache, you should associate them with a session object, eg.

obj_from_cache = get_from_cache(key)
session.merge(obj_from_cache)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文