开发者

Working with Beaker Cache and SQLAlchemy

开发者 https://www.devze.com 2023-02-11 07:14 出处:网络
I\'m trying to use beaker cache with SQLAlchemy but I\'ve been receiving errors. Here are my table definitions.

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开发者_如何学C 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?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消