[英]SqlAlchemy Outer Join Only Returns One Table
因此,當我運行時,在select * from table1 t1 left outer join table2 t2 on t1.id = t2.id;
在sqlite3
終端中
我可以按照期望返回數據。
但是,當我在SqlAlchemy中運行TableOneModel.query.outerjoin(TableTwoModel,TableOneModel.id == TableTwoModel.id)
我只得到table1
信息。 我什至沒有從table2
獲得空列。 我想念一些愚蠢的東西嗎?
您可能正在使用Flask-SQLAlchemy,它提供了查詢屬性作為選擇模型實體的快捷方式。 您的查詢相當於
db.session.query(TableOneModel).\
join(TableTwoModel,TableOneModel.id == TableTwoModel.id)
顯式查詢兩個實體:
db.session.query(TableOneModel, TableTwoModel).\
join(TableTwoModel,TableOneModel.id == TableTwoModel.id)
TableOneModel.query.\
join(TableTwoModel,TableOneModel.id == TableTwoModel.id).\
add_entity(TableTwoModel)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.