【问题标题】:Working with Beaker Cache and SQLAlchemy使用烧杯缓存和 SQLAlchemy
【发布时间】:2011-06-29 08:04:16
【问题描述】:

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

我做错了什么?

【问题讨论】:

    标签: python caching sqlalchemy beaker


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多