Question

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?

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top