即使密钥不存在,重复键值也会违反postgres中的唯一约束?

时间:2016-10-03 13:37:29

标签: postgresql spring-boot

我使用的查询如下 -

Key (id)=(2556) already exists.; nested exception is org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "mytable_modified_pkey

该表的主键是mytable.id 运行此查询时,我一直看到以下错误 -

SELECT tags.tag_id, count(1) from (SELECT distinct uttv.id, true, false, uttv.type, false, uttv.created_at 
                 FROM userTable tt WHERE booleantwo = false 
                    AND type is not null 
                    AND tt.id in ( 
          SELECT id from userTable WHERE status=12 AND  booleantwo=false AND booleanThree=false UNION SELECT id FROM userTable WHERE status=19
                       ) as tags group by tags.tag_id having count(1) > 1

当我检查mytable时,我没有看到任何与2556匹配的id。

此查询作为spring boot项目的一部分运行,我将隔离级别设置为Seriablizable。

我认为在查询中使用UNION会确保没有重复的id,然后在外部SELECT查询中使用distinct将进一步确保我们只插入非重复项。我知道这个错误正在发生,因为选择查询可能会返回一些重复项,但即便如此,你会看到我正在使用 - " AND NOT EXISTS"并确保如果id已经存在,请不要插入。

我为什么看到这个错误的任何输入都将非常感激。这个查询也很慢。

我还尝试通过按ID分组执行查询的SELECT部分​​,并检查是否有任何ID出现多次,但我没有看到任何记录。现在我可以看到有重复。

{{1}}

1 个答案:

答案 0 :(得分:2)

我要说SELECT会返回多行,并且employee上存在唯一约束的列的值相同。

相关问题