【发布时间】:2015-12-22 20:29:03
【问题描述】:
我需要将 2 个表从生产复制到开发。它们是相同的,只是生产表中有更多数据。我尝试pg_dump 从生产转储并复制到开发,但它抱怨外键约束。如何暂时关闭约束?我不想复制整个数据库,因为它的数据太多,我想保留我的测试数据和用户。
ALTER TABLE
DROP INDEX
ERROR: cannot drop constraint foods_pkey on table foods because other objects depend on it
DETAIL: constraint fk_rails_7e399284de on table histories depends on index foods_pkey
constraint fk_rails_8d89280489 on table food_nutrients depends on index foods_pkey
这是两张表:
Table "public.foods"
Column | Type | Modifiers
------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('foods_id_seq'::regclass)
...
Indexes:
"foods_pkey" PRIMARY KEY, btree (id)
"index_foods_on_food_category_id" btree (food_category_id)
Foreign-key constraints:
"fk_rails_a28abb337f" FOREIGN KEY (food_category_id) REFERENCES food_categories(id)
Referenced by:
TABLE "histories" CONSTRAINT "fk_rails_7e399284de" FOREIGN KEY (food_id) REFERENCES foods(id)
TABLE "food_nutrients" CONSTRAINT "fk_rails_8d89280489" FOREIGN KEY (food_id) REFERENCES foods(id)
和
Table "public.food_categories"
Column | Type | Modifiers
------------+-----------------------------+--------------------------------------------------------------
id | integer | not null default nextval('food_categories_id_seq'::regclass)
...
Indexes:
"food_categories_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "foods" CONSTRAINT "fk_rails_a28abb337f" FOREIGN KEY (food_category_id) REFERENCES food_categories(id)
我也试过--disable-triggers,但没有帮助。
【问题讨论】:
-
您只想从
food和food_categories获取数据?,您如何需要它,例如在开发数据库中创建新表并将数据从生产数据库复制到它或什么? -
你见过这个 - stackoverflow.com/questions/3195125/… 吗?
-
@wingᴇdpᴀnᴛʜᴇʀ 通过使用
pg_dump | psql -
如果是常规操作(从 prod 复制到 dev),您可以将您的约束(FK)声明为 DEFERRABLE,并且可以在
copy事务结束时进行检查。请看这个优秀的answer -
@Houari 但是我不必使用 DEFERRABLE 创建约束吗?如果约束和索引已经存在怎么办?如何删除它和表格以便重新创建它?
标签: postgresql