简体   繁体   English

为什么这个查询不起作用(EXISTS 的问题)

[英]Why this query doesn't work (problem with EXISTS)

I'm struggling with such query (generated by sqlalchemy. I've renamed it for clarity)我正在努力处理这样的查询(由 sqlalchemy 生成。为清楚起见,我已将其重命名)

SELECT table.x + :dx AS cx, table.y + :dy AS cy 
FROM table
WHERE NOT (EXISTS (SELECT * FROM table AS table_1
                   WHERE table_1.x = cx AND table_1.y = cy))
  AND     (EXISTS (SELECT * FROM table2
                   WHERE table2.table_id = table.id))

For dx, dy = 1, 1 and tables:对于 dx、dy = 1、1 和表格:

 Table:
  id | x | y
 ----+---+---
   1 | 0 | 0

 Table2:
  id | table_id
 ----+----------
  1  | 1

It should return:它应该返回:

  cx | cy
 ----+----
   1 |  1

However the second condition seems to return false.然而,第二个条件似乎返回假。

EDIT: It seems to be a problem with transaction flushing in SQLAlchemy.编辑: SQLAlchemy 中的事务刷新似乎是一个问题。 I thought that in case of executing query (non-ORM mapped) it will automatically flush.我认为如果执行查询(非 ORM 映射),它会自动刷新。 However it seems that it is not flushed in such case or flushed in incorrect order.但是,在这种情况下,它似乎没有被刷新或以不正确的顺序刷新。 In any case adding additional model.DBSession.flush() seems to fixed the problem.无论如何,添加额外model.DBSession.flush()似乎可以解决问题。

As written in edit the problem was in flushing.正如编辑中所写,问题出在冲洗中。

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

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