我有两个模型Employee和UnionEmployees。 我可以像这样基于employeeid加入这两个:
query = session.query(Employee,UnionEmployees)\
.filter(Employee.employeeid==UnionEmployees.employeeid)\
.filter_by(union_status='active')
使用statement.compile
查看引擎下的SQLsql = str(query.statement.compile(dialect=mysql.dialect()))
我看到select语句看起来像这样:
select Employee.employeeid, Employee.first_name....UnionEmployee.employeeId, UnionEmployee.first_name...
...
from Employee join UnionEmployee
on Employee.employeeid = UnionEmployee.employeeid
where UnionEmployee.union_status = 'active'
有没有办法改变查询,以便从中编译的sql语句只执行单个实体的选择而不是两者?如果我确定我只想要来自一个实体的用户,我想避免获得冗余数据。
答案 0 :(得分:1)
只需使用显式连接:
query(Employee).join(UnionEmployees, Employee.employeeid == UnionEmployee.employeeid).filter(...)