Prisma创建迁移基线失败:字段重复报错的排查与解决
问题描述
我有如下PostgreSQL表:
Table "public.topic" Column | Type | Modifiers | Storage | Stats target | Description ----------------+--------------------------+----------------------------------------------------+----------+--------------+------------- id | integer | not null default nextval('topic_id_seq'::regclass) | plain | | parent_topicid | integer | | plain | | topic | character varying | | extended | | public | boolean | default true | plain | | creator | character varying(12) | collate et_EE default NULL::character varying | extended | | created | timestamp with time zone | not null default now() | plain | | updater | character varying(12) | collate et_EE default NULL::character varying | extended | | updated | timestamp with time zone | default now() | plain | | Indexes: "topic_pkey" PRIMARY KEY, btree (id) "topic_parent_topicid_idx" btree (parent_topicid) Foreign-key constraints: "topics_have_parent" FOREIGN KEY (parent_topicid) REFERENCES topic(id) ON DELETE SET NULL Referenced by: TABLE "book.book_topic" CONSTRAINT "book_topic_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "card.card_topic" CONSTRAINT "card_topic_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "print.print_topic" CONSTRAINT "print_topic_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "antique.antique_topic" CONSTRAINT "topic_antique_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "lp.lp_topic" CONSTRAINT "topic_lp_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "stationary.stationary_topic" CONSTRAINT "topic_stationary_belongs_to_topic" FOREIGN KEY (topicid) REFERENCES topic(id) ON DELETE CASCADE TABLE "topic" CONSTRAINT "topics_have_parent" FOREIGN KEY (parent_topicid) REFERENCES topic(id) ON DELETE SET NULL Triggers: topic_delete AFTER DELETE ON topic FOR EACH ROW EXECUTE PROCEDURE add_into_queue_task() topic_update AFTER UPDATE ON topic FOR EACH ROW EXECUTE PROCEDURE add_into_queue_task() update_topic_updated BEFORE UPDATE ON topic FOR EACH ROW EXECUTE PROCEDURE update_updated_column()
其中parent_topicid是递归外键,指向本表主键。
执行npx prisma db pull后生成的Prisma模型如下:
model topic { id Int @id @default(autoincrement()) parent_topicid Int? topic String? @db.VarChar public Boolean? @default(true) creator String? @db.VarChar(12) created DateTime @default(now()) @db.Timestamptz(6) updater String? @db.VarChar(12) updated DateTime? @default(now()) @db.Timestamptz(6) antique_topic antique_topic[] book_topic book_topic[] card_topic card_topic[] lp_topic lp_topic[] print_topic print_topic[] topic topic? @relation("topicTotopic", fields: [parent_topicid], references: [id], onUpdate: NoAction, map: "topics_have_parent") other_topic topic[] @relation("topicTotopic") stationary_topic stationary_topic[] @@index([parent_topicid]) @@schema("public") }
尝试创建初始迁移基线时执行命令:
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql
触发报错:
Error: P1012 error: Field "topic" is already defined on model "topic". --> schema.prisma:1340 | 1339 | print_topic print_topic[] 1340 | topic topic? @relation("topicTotopic", fields: [parent_topicid], references: [id], onUpdate: NoAction, map: "topics_have_parent") |
我确认数据库表结构无问题,但Prisma对自动生成的Schema报错,请问该如何创建迁移基线?
解决方案
报错核心原因是字段名冲突:表本身存在名为topic的列,而Prisma自动生成的递归关系字段也被命名为topic,导致模型中出现重复字段。
解决步骤如下:
修改Prisma模型中的递归关系字段名,避免与现有列名重复
将模型中这两行:topic topic? @relation("topicTotopic", fields: [parent_topicid], references: [id], onUpdate: NoAction, map: "topics_have_parent") other_topic topic[] @relation("topicTotopic")修改为更清晰的命名,比如:
parent_topic topic? @relation("TopicHierarchy", fields: [parent_topicid], references: [id], onUpdate: NoAction, map: "topics_have_parent") child_topics topic[] @relation("TopicHierarchy")注意需同步修改关系名称(也可保留原关系名,但字段名必须修改)。
验证修改后的模型合法性
执行npx prisma validate命令,确认Schema无语法错误。重新生成迁移脚本
再次执行之前的命令:npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql此时即可成功生成迁移基线。
(可选)更新Prisma客户端
执行npx prisma generate,确保后续可正常使用修改后的关系字段进行查询。
内容的提问来源于stack exchange,提问作者w.k
相关产品推荐
相关产品推荐

