级联删除无法使用具有 1:1 关系的 SQLite
Cascade delete not working SQLite with 1:1 relationship
我正在尝试在两个 table Places
和 People
之间建立 1:1 关系。一个人有一个家,当那个人被删除时,家也应该被删除。其他 table 也使用 Places
table,因此 Places
table 中没有引用 People
[=31] 的列=].
为了尝试实现这一点,我设置了 People
table 以便在删除一行时,在指向 [=13 的外键上进行级联删除=] table 行也被删除。
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
然而,当我实际尝试这样做时,Places
table 中的行仍然存在。有什么办法可以解决这个问题吗?
完全可运行的示例
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS "Places" (
"Id" TEXT NOT NULL CONSTRAINT "PK_Places" PRIMARY KEY,
"Name" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
DELETE FROM Places;
DELETE FROM People;
INSERT INTO "Places" ("Id", "Name") VALUES ("6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Castle");
INSERT INTO "People" ("Id", "HomeId", "Name") VALUES ("ccb079ce-b477-47cf-adba-9fdac6a41718", "6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Fiona");
-- Should delete both the person and the place, but does not
DELETE FROM "People" WHERE "Id" = "ccb079ce-b477-47cf-adba-9fdac6a41718";
SELECT pl.Name "Place Name",
po.Name "Person Name"
FROM Places pl
LEFT JOIN People po USING(Name)
UNION ALL
SELECT pl.Name,
po.Name
FROM People po
LEFT JOIN Places pl USING(Name)
WHERE pl.Name IS NULL;
您在 table People
中为引用列 Id
的列 HomeId
定义的外键的 "ON DELETE CASCADE"
操作table Places
表示:
whenever you delete a row in the table Places
(which is the parent
table in this relationship) all rows in the table People
that hold a
reference to the deleted row will also be deleted.
参见demo。
在您的情况下,您要删除 table People
中的一行,这根本不会影响 table Places
.
我正在尝试在两个 table Places
和 People
之间建立 1:1 关系。一个人有一个家,当那个人被删除时,家也应该被删除。其他 table 也使用 Places
table,因此 Places
table 中没有引用 People
[=31] 的列=].
为了尝试实现这一点,我设置了 People
table 以便在删除一行时,在指向 [=13 的外键上进行级联删除=] table 行也被删除。
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
然而,当我实际尝试这样做时,Places
table 中的行仍然存在。有什么办法可以解决这个问题吗?
完全可运行的示例
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS "Places" (
"Id" TEXT NOT NULL CONSTRAINT "PK_Places" PRIMARY KEY,
"Name" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
DELETE FROM Places;
DELETE FROM People;
INSERT INTO "Places" ("Id", "Name") VALUES ("6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Castle");
INSERT INTO "People" ("Id", "HomeId", "Name") VALUES ("ccb079ce-b477-47cf-adba-9fdac6a41718", "6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Fiona");
-- Should delete both the person and the place, but does not
DELETE FROM "People" WHERE "Id" = "ccb079ce-b477-47cf-adba-9fdac6a41718";
SELECT pl.Name "Place Name",
po.Name "Person Name"
FROM Places pl
LEFT JOIN People po USING(Name)
UNION ALL
SELECT pl.Name,
po.Name
FROM People po
LEFT JOIN Places pl USING(Name)
WHERE pl.Name IS NULL;
您在 table People
中为引用列 Id
的列 HomeId
定义的外键的 "ON DELETE CASCADE"
操作table Places
表示:
whenever you delete a row in the table
Places
(which is the parent table in this relationship) all rows in the tablePeople
that hold a reference to the deleted row will also be deleted.
参见demo。
在您的情况下,您要删除 table People
中的一行,这根本不会影响 table Places
.