如何在Postgres中仅复制2个表并忽略外键和约束?

时间:2015-09-25 04:32:34

标签: postgresql

我需要将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,但没有帮助。

1 个答案:

答案 0 :(得分:0)

您可以先复制“food_categories”表。之后,您可以复制“食物”表。