首页 > 解决方案 > 如何使用 psql 恢复具有关系的数据库?

问题描述

我正在尝试使用 psql 恢复数据库。但似乎 psql 在遇到时总是失败CONSTRAINT。在我查看垃圾场之后。我发现子表,即持有的表FOREIGN KEY,是在父表之前创建的。

这是片段...

DROP TABLE IF EXISTS "answer";
DROP SEQUENCE IF EXISTS answer_id_seq;
CREATE SEQUENCE answer_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 71 CACHE 1;

CREATE TABLE "public"."answer" (
    "id" integer DEFAULT nextval('answer_id_seq') NOT NULL,
    "text" character varying NOT NULL,
    "weight" double precision NOT NULL,
    "questionId" integer NOT NULL,
    "baseCreated" timestamp DEFAULT now() NOT NULL,
    "baseUpdated" timestamp DEFAULT now() NOT NULL,
    CONSTRAINT "PK_9232db17b63fb1e94f97e5c224f" PRIMARY KEY ("id"),
    CONSTRAINT "FK_a4013f10cd6924793fbd5f0d637" FOREIGN KEY ("questionId") REFERENCES question(id) ON DELETE CASCADE NOT DEFERRABLE
) WITH (oids = false);

psql 命令。

psql -h 0.0.0.0 -p 5432 -U foobar -1 foobar < foobar.sql

和错误。

NOTICE:  table "answer" does not exist, skipping
DROP TABLE
NOTICE:  sequence "answer_id_seq" does not exist, skipping
DROP SEQUENCE
CREATE SEQUENCE
ERROR:  relation "question" does not exist
ERROR:  relation "answer" does not exist
ERROR:  relation "answer" does not exist
LINE 1: INSERT INTO "answer" ("id", "text", "weight", "questionId", ...

标签: postgresql

解决方案


推荐阅读