为什么 drop table cascade 没有删除 postgresql 中的 child table?
Why drop table cascade is not removing child table in postgresql?
我有以下两个 tables,其架构如下所示:-
postgres=# \d products1;
Table "public.products1"
Column | Type | Modifiers
--------------------+---------+--------------------------------------------------------
id | integer | not null default nextval('products1_id_seq'::regclass)
name | text | not null
default_picture_id | integer |
Indexes:
"products1_pkey" PRIMARY KEY, btree (id)
"unique_id_default_pic_id" UNIQUE CONSTRAINT, btree (id, default_picture_id)
Foreign-key constraints:
"fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT
Referenced by:
TABLE "product_pictures1" CONSTRAINT "fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)
postgres=# \d product_pictures1;
Table "public.product_pictures1"
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('product_pictures1_id_seq'::regclass)
img_path | text | not null
product_id | integer |
Indexes:
"product_pictures1_pkey" PRIMARY KEY, btree (id)
"unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)
Foreign-key constraints:
"fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)
Referenced by:
TABLE "products1" CONSTRAINT "fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT
以下两个 table 互相推荐:-
当我尝试删除任何一个 table 时,出现以下错误:-
postgres=# drop table products1;
ERROR: cannot drop table products1 because other objects depend on it
DETAIL: constraint fk_id_product_id on table product_pictures1 depends on table products1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
但是当我用级联选项删除时,table被删除了,但它不会删除另一个table或其在table中的外键列,它只删除外键约束。
postgres=# drop table products1 cascade;
NOTICE: drop cascades to constraint fk_id_product_id on table product_pictures1
DROP TABLE
postgres=# \d product_pictures1;
Table "public.product_pictures1"
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('product_pictures1_id_seq'::regclass)
img_path | text | not null
product_id | integer |
Indexes:
"product_pictures1_pkey" PRIMARY KEY, btree (id)
"unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)
这是预期的行为吗?在 on delete cascade
的情况下,删除父行中的行,在子行中删除 table,但同样的事情不会发生在 drop table ?
我错过了什么吗?这种行为是 postgres 特有的吗?
提前致谢。
(...) to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified. (CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.)
https://www.postgresql.org/docs/current/static/sql-droptable.html(强调我的)
因为 DROP ... CASCADE
就是这样设计的。
but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely
(强调我的)
这不是 Postgres 特有的。删除表时,Oracle 和 DB2 的工作方式相同。
我有以下两个 tables,其架构如下所示:-
postgres=# \d products1;
Table "public.products1"
Column | Type | Modifiers
--------------------+---------+--------------------------------------------------------
id | integer | not null default nextval('products1_id_seq'::regclass)
name | text | not null
default_picture_id | integer |
Indexes:
"products1_pkey" PRIMARY KEY, btree (id)
"unique_id_default_pic_id" UNIQUE CONSTRAINT, btree (id, default_picture_id)
Foreign-key constraints:
"fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT
Referenced by:
TABLE "product_pictures1" CONSTRAINT "fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)
postgres=# \d product_pictures1;
Table "public.product_pictures1"
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('product_pictures1_id_seq'::regclass)
img_path | text | not null
product_id | integer |
Indexes:
"product_pictures1_pkey" PRIMARY KEY, btree (id)
"unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)
Foreign-key constraints:
"fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)
Referenced by:
TABLE "products1" CONSTRAINT "fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT
以下两个 table 互相推荐:-
当我尝试删除任何一个 table 时,出现以下错误:-
postgres=# drop table products1;
ERROR: cannot drop table products1 because other objects depend on it
DETAIL: constraint fk_id_product_id on table product_pictures1 depends on table products1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
但是当我用级联选项删除时,table被删除了,但它不会删除另一个table或其在table中的外键列,它只删除外键约束。
postgres=# drop table products1 cascade;
NOTICE: drop cascades to constraint fk_id_product_id on table product_pictures1
DROP TABLE
postgres=# \d product_pictures1;
Table "public.product_pictures1"
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('product_pictures1_id_seq'::regclass)
img_path | text | not null
product_id | integer |
Indexes:
"product_pictures1_pkey" PRIMARY KEY, btree (id)
"unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)
这是预期的行为吗?在 on delete cascade
的情况下,删除父行中的行,在子行中删除 table,但同样的事情不会发生在 drop table ?
我错过了什么吗?这种行为是 postgres 特有的吗?
提前致谢。
(...) to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified. (CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.)
https://www.postgresql.org/docs/current/static/sql-droptable.html(强调我的)
因为 DROP ... CASCADE
就是这样设计的。
but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely
(强调我的)
这不是 Postgres 特有的。删除表时,Oracle 和 DB2 的工作方式相同。