[英]SQLAlchemy - Adding where clauses to a select generates subquery
Select.where
的文档说该方法应该在现有的SELECT查询后附加WHERE
子句,但有时似乎将select包装为子查询,从而导致ProgrammingError: (psycopg2.ProgrammingError) subquery in FROM must have an alias
错误。 这似乎仅在我尝试过滤的选择在查询链中较早出现联接时才会发生
# 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'")
我想我只是想通了。 问题似乎是自参考列,所以
filtered = my_select.where(my_select.c.my_col == 'foo')
应该是
filtered = my_select.where(tbl2.c.my_col == 'foo')
因此它引用的是原始列对象,而不是select中的列。
这只是一个假设,因此,如果有人也可以验证,将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.