views:

25

answers:

0

Hello,

I have a complex database where user object is related almost to every table in some way. In a previous post, I asked how to get read-only objects. I found out I could use the option "joinedload" when I query database. It's working fine, but I have another problem.

These are my tables:

class User(rdb.Model):
    """Set up users table in the database"""
    rdb.metadata(metadata)
    rdb.tablename("users")

    id = Column("id", Integer, primary_key=True)
    name = Column("name", String(50))
    email = Column("email", String(50))

    group = relationship("UserGroup")
    channels = relationship("Channel", secondary=user_channels, order_by="Channel.titleView", backref="users")
    mediaGroups = relationship("MediaGroup", secondary=user_media_groups, order_by="MediaGroup.title", backref="users")

class UserGroup(rdb.Model):
    """Set up user_groups table in the database"""
    rdb.metadata(metadata)
    rdb.tablename("user_groups")

    id = Column("id", Integer, primary_key=True)
    title = Column("title", String(50))

    users = relationship("User", order_by="User.name", backref="user_groups")
    permissions = relationship("Permission", secondary=user_group_permissions, backref="user_groups")

When I do:

user = session.query(User).options(joinedload("group"), joinedload("channels"), joinedload("mediaGroups")).get(int(userId))

And when I try to get the users from the attribute group in user, I get this:

"DetachedInstanceError: Parent instance is not bound to a Session; lazy load operation of attribute 'users' cannot proceed"

So how can I get all the data related to user object without having this problem? I save my user object in the session object in the server, so I need in just one time to get all the data related to user.

Thanks in advance!

Alvaro