1

我正在尝试将烧杯缓存与 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')

但这发生在模板(post.user.fullname)中的以下错误;

AttributeError: 'AppenderQuery' object has no attribute 'fullname'

我究竟做错了什么?

4

1 回答 1

2

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

obj_from_cache = get_from_cache(key)
session.merge(obj_from_cache)
于 2011-06-02T12:04:48.507 回答