如何在sqlalchemy会话查询中加入联接? [英] how to do a join in sqlalchemy session query?

查看:87
本文介绍了如何在sqlalchemy会话查询中加入联接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在sqlalchemy中找到此查询的等效项.

I need to find the equivalent of this query in 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'

谁能举一个表映射到新类对象的联接查询示例?

can anyone give an example of join query with tables mapped to new class objects ?

推荐答案

尝试此操作,假设您的用户映射器与配置的国家/地区有关系.

Try this, assuming your User mapper has a relationship to Country configured.

user, country = session.query(User, Country.country).join(Country).filter(User.user_email == 'abc@def.com').first()

这篇关于如何在sqlalchemy会话查询中加入联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆