SQLAlchemy - 向select添加where子句会生成子查询

时间:2016-02-22 10:44:30

标签: python postgresql sqlalchemy

Select.where WHERE表示此方法应该在现有SELECT查询中附加ProgrammingError: (psycopg2.ProgrammingError) subquery in FROM must have an alias子句,但有时它似乎将select包装为子查询,从而导致# this works my_select = select([tbl.c.id, tbl.c.my_col]) filtered = my_select.where(tbl.c.my_col == 'foo') # this doesn't work, and seems to wrap the my_select in an additional subquery j = tbl1.join(tbl2, tbl1.c.id == tbl2.c.id) my_select = select([tbl1.c.id, tbl2.c.my_col]).select_from(j) filtered = my_select.where(my_select.c.my_col == 'foo') # Text representations will usually work as expected j = tbl1.join(tbl2, tbl1.c.id == tbl2.c.id) my_select = select([tbl1.c.id, tbl2.c.my_col]).select_from(j) filtered = my_select.where("my_col = 'foo'") 错误。if ($("#_sku").val() == '' && $("#product-type").val() == 'simple') 这种情况似乎只发生在尝试过滤的选择我之前在查询链中有一个连接

db.php

1 个答案:

答案 0 :(得分:0)

我想我只想出了这个。问题似乎是自引用列,所以

filtered = my_select.where(my_select.c.my_col == 'foo')

应该是

filtered = my_select.where(tbl2.c.my_col == 'foo')

以便它引用原始列对象而不是select中的列。

这只是一个假设,如果其他人也可以验证,也会很感激。