简体   繁体   English

SqlAlchemy外部联接仅返回一个表

[英]SqlAlchemy Outer Join Only Returns One Table

So when I run select * from table1 t1 left outer join table2 t2 on t1.id = t2.id; 因此,当我运行时,在select * from table1 t1 left outer join table2 t2 on t1.id = t2.id; in sqlite3 terminal sqlite3终端中

I get the data back as I want and would expect. 我可以按照期望返回数据。

However, when I run this in SqlAlchemy TableOneModel.query.outerjoin(TableTwoModel,TableOneModel.id == TableTwoModel.id) 但是,当我在SqlAlchemy中运行TableOneModel.query.outerjoin(TableTwoModel,TableOneModel.id == TableTwoModel.id)

I only get table1 information back. 我只得到table1信息。 I don't even get empty columns from table2 . 我什至没有从table2获得空列。 Am I missing something silly? 我想念一些愚蠢的东西吗?

You're probably using Flask-SQLAlchemy, which provides the query property as a shortcut for selecting model entities. 您可能正在使用Flask-SQLAlchemy,它提供了查询属性作为选择模型实体的快捷方式。 Your query is equivalent to 您的查询相当于

db.session.query(TableOneModel).\
    join(TableTwoModel,TableOneModel.id == TableTwoModel.id)

Either explicitly query for both entities: 显式查询两个实体:

db.session.query(TableOneModel, TableTwoModel).\
    join(TableTwoModel,TableOneModel.id == TableTwoModel.id)

or add the entity to your original: 将实体添加到您的原始实体中:

TableOneModel.query.\
    join(TableTwoModel,TableOneModel.id == TableTwoModel.id).\
    add_entity(TableTwoModel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM