为什么我的级联删除在 PostgreSQL 中不起作用?

Why doesn't my cascading delete work in PostgreSQL?

我正在尝试在 PostgreSQL 中配置级联删除并创建了这两个表:

CREATE TABLE "public"."User" (
  id SERIAL PRIMARY KEY,
  name TEXT
);

CREATE TABLE "public"."Post" (
  id SERIAL PRIMARY KEY,
  title TEXT,
  author INTEGER,
  FOREIGN KEY (author) REFERENCES "public"."User" (id) ON DELETE CASCADE
);

现在,在用

创建了两条相关记录之后
INSERT INTO "public"."User"("name") VALUES('Alice') RETURNING "id", "name";
INSERT INTO "public"."AnotherPost"("title", "author") VALUES('Hello World', 1) RETURNING "id", "title", "author";

我希望在删除 Post 记录时,相应的 User 记录也会被删除。

DELETE FROM "public"."Post" WHERE "id"=1;

但是,我创建的 User 记录仍然存在。有人可以告诉我我缺少什么或为什么这不起作用吗?

on delete cascade 外键以相反的方式工作。 IE。如果您删除用户,其帖子也会被删除。