如何在sqlalchemy会话查询中进行连接?
我需要在sqlalchemy中找到这个查询的等价写法。
SELECT u.user_id, u.user_name, c.country FROM
table_user u , table_country c WHERE u.user_email = 'abc@def.com'
我试了下面的代码:
session.query(User).join(Country.country).filter(User.user_email == 'abc@def.com').first()
但是我遇到了下面的错误:
AttributeError: 'ColumnProperty' object has no attribute 'mapper'
有没有人能给我一个关于如何用映射到新类对象的表进行连接查询的例子?
1 个回答
20
试试这个,前提是你的用户映射器已经设置了与国家的关系。
user, country = session.query(User, Country.country).join(Country).filter(User.user_email == 'abc@def.com').first()