Postgresql:在多次更新查询之后,如何验证约束?

Postgresql:在多次更新查询之后,如何验证约束?,postgresql,sql-update,unique-constraint,Postgresql,Sql Update,Unique Constraint,我正在用postgresql建立一个数据库 我有3个表通过多对多关系连接在一起。联接表uiComponent\u cat\u subcat使用subcategoryId、categoryId和位置定义了一个唯一的约束 在我的工作流程中,我必须连续更新两个查询。第一个会破坏约束,而第二个会将约束设置回原位 我希望在两次查询之后验证此约束 ALTER TABLE "uiComponent_cat_subcat" ALTER CONSTRAINT "unique_constraint_categor

我正在用postgresql建立一个数据库

我有3个表通过多对多关系连接在一起。联接表
uiComponent\u cat\u subcat
使用
subcategoryId
categoryId
位置
定义了一个唯一的约束

在我的工作流程中,我必须连续更新两个查询。第一个会破坏约束,而第二个会将约束设置回原位

我希望在两次查询之后验证此约束

ALTER TABLE "uiComponent_cat_subcat" ALTER CONSTRAINT "unique_constraint_categoryId_subcategoryId_position" DEFERRABLE INITIALLY DEFERRED;
我尝试对约束使用
delerable
属性,并将查询封装在一个事务中。但是,这只能应用于设置为外键的约束

在进行两次更新查询后,如何验证唯一约束

编辑:以下是对象定义:

1-创建表

CREATE TABLE IF NOT EXISTS "uiComponent" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "preview"      json,
  "symbolID"     varchar(255),
  "libraryID"    varchar(255),
  "type"         "public"."enum_uiComponent_type" DEFAULT 'basic',
  "content"      json,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "category" 
(
  "id"            uuid NOT NULL,
  "name"          varchar(255),
  "immuable"      boolean,
  "description"   varchar(255),
  "section"       "public"."enum_category_section" NOT NULL DEFAULT 'Components',
  "created_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "subcategory" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "position"     integer NOT NULL,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "uiComponent_cat_subcat" 
(
  "id"              uuid,
  "uiComponentId"   uuid NOT NULL REFERENCES "uiComponent" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "categoryId"      uuid NOT NULL REFERENCES "category" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "subcategoryId"   uuid REFERENCES "subcategory" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
  "position"        integer NOT NULL,
  PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "uiComponent" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "preview"      json,
  "symbolID"     varchar(255),
  "libraryID"    varchar(255),
  "type"         "public"."enum_uiComponent_type" DEFAULT 'basic',
  "content"      json,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "category" 
(
  "id"            uuid NOT NULL,
  "name"          varchar(255),
  "immuable"      boolean,
  "description"   varchar(255),
  "section"       "public"."enum_category_section" NOT NULL DEFAULT 'Components',
  "created_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "subcategory" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "position"     integer NOT NULL,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "uiComponent_cat_subcat" 
(
  "id"              uuid,
  "uiComponentId"   uuid NOT NULL REFERENCES "uiComponent" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "categoryId"      uuid NOT NULL REFERENCES "category" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "subcategoryId"   uuid REFERENCES "subcategory" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
  "position"        integer NOT NULL,
  PRIMARY KEY ("id")
);
2-创建约束

ALTER TABLE "uiComponent_cat_subcat" 
   ADD CONSTRAINT "unique_constraint_categoryId_subcategoryId_position" UNIQUE ("categoryId", "subcategoryId", "position") 
   DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "uiComponent_cat_subcat" 
   ADD CONSTRAINT "unique_constraint_categoryId_subcategoryId_position" UNIQUE ("categoryId", "subcategoryId", "position") 
   DEFERRABLE INITIALLY DEFERRED;
3-更新查询

START TRANSACTION;

UPDATE "uiComponent_cat_subcat" 
  SET "position"="position"+ 1 
WHERE "position" BETWEEN 1 AND 1 
AND "categoryId" = '10e4621e-f52d-4fe2-a408-139841718fd5' 
AND "subcategoryId" = 'a7770326-35be-45ae-ba26-4635cfb6f4dc';

UPDATE "uiComponent_cat_subcat" 
  SET "position"=1 
WHERE "id" = '50022f87-8fe9-4f21-a622-d5f51c16d9fc'

COMMIT;
START TRANSACTION;

UPDATE "uiComponent_cat_subcat" 
  SET "position"="position"+ 1 
WHERE "position" BETWEEN 1 AND 1 
AND "categoryId" = '10e4621e-f52d-4fe2-a408-139841718fd5' 
AND "subcategoryId" = 'a7770326-35be-45ae-ba26-4635cfb6f4dc';

UPDATE "uiComponent_cat_subcat" 
  SET "position"=1 
WHERE "id" = '50022f87-8fe9-4f21-a622-d5f51c16d9fc'

COMMIT;
问题解决了,但我不知道为什么1-创建表

CREATE TABLE IF NOT EXISTS "uiComponent" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "preview"      json,
  "symbolID"     varchar(255),
  "libraryID"    varchar(255),
  "type"         "public"."enum_uiComponent_type" DEFAULT 'basic',
  "content"      json,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "category" 
(
  "id"            uuid NOT NULL,
  "name"          varchar(255),
  "immuable"      boolean,
  "description"   varchar(255),
  "section"       "public"."enum_category_section" NOT NULL DEFAULT 'Components',
  "created_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "subcategory" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "position"     integer NOT NULL,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "uiComponent_cat_subcat" 
(
  "id"              uuid,
  "uiComponentId"   uuid NOT NULL REFERENCES "uiComponent" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "categoryId"      uuid NOT NULL REFERENCES "category" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "subcategoryId"   uuid REFERENCES "subcategory" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
  "position"        integer NOT NULL,
  PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "uiComponent" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "preview"      json,
  "symbolID"     varchar(255),
  "libraryID"    varchar(255),
  "type"         "public"."enum_uiComponent_type" DEFAULT 'basic',
  "content"      json,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "category" 
(
  "id"            uuid NOT NULL,
  "name"          varchar(255),
  "immuable"      boolean,
  "description"   varchar(255),
  "section"       "public"."enum_category_section" NOT NULL DEFAULT 'Components',
  "created_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"    timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "subcategory" 
(
  "id"           uuid NOT NULL,
  "name"         varchar(255),
  "position"     integer NOT NULL,
  "created_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  "updated_at"   timestamp WITH time ZONE NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "uiComponent_cat_subcat" 
(
  "id"              uuid,
  "uiComponentId"   uuid NOT NULL REFERENCES "uiComponent" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "categoryId"      uuid NOT NULL REFERENCES "category" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
  "subcategoryId"   uuid REFERENCES "subcategory" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
  "position"        integer NOT NULL,
  PRIMARY KEY ("id")
);
2-创建约束

ALTER TABLE "uiComponent_cat_subcat" 
   ADD CONSTRAINT "unique_constraint_categoryId_subcategoryId_position" UNIQUE ("categoryId", "subcategoryId", "position") 
   DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "uiComponent_cat_subcat" 
   ADD CONSTRAINT "unique_constraint_categoryId_subcategoryId_position" UNIQUE ("categoryId", "subcategoryId", "position") 
   DEFERRABLE INITIALLY DEFERRED;
3-更新查询

START TRANSACTION;

UPDATE "uiComponent_cat_subcat" 
  SET "position"="position"+ 1 
WHERE "position" BETWEEN 1 AND 1 
AND "categoryId" = '10e4621e-f52d-4fe2-a408-139841718fd5' 
AND "subcategoryId" = 'a7770326-35be-45ae-ba26-4635cfb6f4dc';

UPDATE "uiComponent_cat_subcat" 
  SET "position"=1 
WHERE "id" = '50022f87-8fe9-4f21-a622-d5f51c16d9fc'

COMMIT;
START TRANSACTION;

UPDATE "uiComponent_cat_subcat" 
  SET "position"="position"+ 1 
WHERE "position" BETWEEN 1 AND 1 
AND "categoryId" = '10e4621e-f52d-4fe2-a408-139841718fd5' 
AND "subcategoryId" = 'a7770326-35be-45ae-ba26-4635cfb6f4dc';

UPDATE "uiComponent_cat_subcat" 
  SET "position"=1 
WHERE "id" = '50022f87-8fe9-4f21-a622-d5f51c16d9fc'

COMMIT;

问题解决了,但我不知道为什么我看不到问题。使用延迟外键,您可以在更改其他表后更新
uiComponent\u cat\u subcat
。只是不要使用级联更新。此外,如果您正在更新主键,则说明您做错了什么。但是您最好通过显示要运行的SQL语句和表定义来进行更详细的解释?类别似乎在功能上依赖于子类别(位置是多余的,但可能是另一个候选键的一部分)@LaurenzAlbe谢谢。如何在同一查询中递增多行并更新一行?PS:我写了所有的查询来回答这个问题@joop一个组件可以在几个类别/子类别中,并且在每个类别中有不同的位置。另外,“为什么
componentId
不在
uiComponent\u cat\u subcat
的PK中”是什么意思
uiComponent\u cat\u subcat
可以有几个
uiComponentId
初始延迟是它现在起作用的关键原因。@a_horse_,没有名字,非常感谢