小编Pet*_*ryl的帖子

避免原子交易中的唯一违规

可以在 PostgreSQL 中创建原子事务吗?

考虑我有这些行的表类别:

id|name
--|---------
1 |'tablets'
2 |'phones'
Run Code Online (Sandbox Code Playgroud)

并且列名具有唯一约束。

如果我尝试:

BEGIN;
update "category" set name = 'phones' where id = 1;
update "category" set name = 'tablets' where id = 2;
COMMIT;
Run Code Online (Sandbox Code Playgroud)

我越来越:

ERROR:  duplicate key value violates unique constraint "category_name_key"
DETAIL:  Key (name)=(tablets) already exists.
Run Code Online (Sandbox Code Playgroud)

postgresql transaction unique-constraint

17
推荐指数
2
解决办法
8218
查看次数