简体   繁体   English

SQLAlchemy从两个表中选择null LEFT JOIN返回空结果

[英]SQLAlchemy select from two tables with null LEFT JOIN returns empty result

I have two sql tables. 我有两个sql表。 Table 1 (id, name) and Table 2 with (id, name, table1_id) 表1(id,name)和表2(id,name,table1_id)

 sql = text('SELECT t1.*, t2.* FROM table1 t1 '
               'LEFT JOIN table2 t2 ON t1.id=t2.table1_id ')
query = db.session.query(Table1, Table2).from_statement(sql).params()
table1_table2_tuple_list = query.all()

If the result of the sql query is 如果sql查询的结果是

67, 'some name', 1, 'some name in table 2', 67

Then everything is OK and I have [(<Table1>, <Table2>)] for table1_table2_tuple_list 然后一切都好,我有[(<Table1>, <Table2>)] for table1_table2_tuple_list

But If the result of the sql query is: (also all other code is for this result) 但是如果sql查询的结果是:(也是所有其他代码都是针对此结果)

67, 'some name', Null, Null, Null

Then instead of receiving [(<Table1>, None)] I receive [(None, None)] 然后我收到[(None, None)]而不是接收[(<Table1>, None)] [(None, None)]

If I change my code a little bit: 如果我稍微改变我的代码:

sql = text('SELECT t1.*, t2.id, t2.name,t1.id FROM table1 t1 '
               'LEFT JOIN table2 t2 ON t1.id=t2.table1_id ')
query = db.session.query(Table1, Table2, Table1.id).from_statement(sql).params()
table1_table2_tuple_list = query.all()

Then I receive: 然后我收到:

[<Table1>, <Table2>, 1]

But in this case I'm not even sure if this is correct because I match two columns from the sql to model Table2 that has three columns. 但在这种情况下,我甚至不确定这是否正确,因为我将sql中的两列匹配到具有三列的Table2模型。 Not sure at all why this is working, but everything seems on place. 不知道为什么这是有效的,但一切似乎都到位。 Still is not what I want, because I don't want to return to this query and to specify again and again new columns if there are such for Table2 仍然不是我想要的,因为我不想返回到这个查询并一次又一次地指定新列,如果有这样的表2

What I need is a way to select from two tables with pure sql , and to match the result to my models. 我需要的是一种从纯sql中选择两个表的方法,并将结果与​​我的模型相匹配。 Also I want to have result even in the cases when one of the tables doesn't have value in it's result columns. 即使在其中一个表的结果列中没有值的情况下,我也希望得到结果。 Hope I am clear. 希望我很清楚。 My goal is to receive 我的目标是接收

[(<Table1>, None)]

when the query is for all colums ( SELECT t1.*, t2.* ) and there is LEFT JOIN with null added. 当查询是针对所有列( SELECT t1.*, t2.* )并且存在添加了null的LEFT JOIN时。

The problem is that your tables have columns with the same names. 问题是您的表具有相同名称的列。 SqlAlchemy after executing your query tries to match resulting columns by names (not by positions) and choose some matching which fits (in this case SA matched columns of table2 with fields of Table1 and vice versa). 执行查询后,SqlAlchemy尝试按名称(而不是位置)匹配结果列,并选择一些匹配的匹配(在这种情况下,表2的SA匹配列与表1的字段相反,反之亦然)。 There is a way to specify matching with .columns() method but I would suggest you to consider using orm-functions of SqlAlchemy instead whenever possible unless you have strong reasons against it. 有一种方法可以指定与.columns()方法的匹配,但我建议你尽可能考虑使用SqlAlchemy的orm-functions ,除非你有充分的理由反对它。

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

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