开发者

querying relation does not give back related object in sqlalchemy

开发者 https://www.devze.com 2023-01-21 22:26 出处:网络
I have a very simple table(mapped as AuthToken class), consisting of a string (\'token\'), and a userid (foreign key to another table), with \'user\' as relation ( = class User)

I have a very simple table(mapped as AuthToken class), consisting of a string ('token'), and a userid (foreign key to another table), with 'user' as relation ( = class User)

session.query(AuthToken.user).one开发者_运维问答() gives back the token, and the userid (as a tuple), but not the user object.

Does anybody know why?

thanks!


You should query mapped classes, not their attributes, if you want to receive objects.

token = Session.query(AuthToken).options(eagerload('user')).filter(...).one()
user = token.user
0

精彩评论

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