我有一个简单的Prisma模式(我只使用相关部分):
enum ApprovalStatus {
APPROVED
DENIED
PENDING
}
model Attendee {
user User @relation(fields: [user_id], references: [id])
user_id BigInt
event Event @relation(fields: [event_id], references: [id])
event_id BigInt
status ApprovalStatus @default(APPROVED)
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
deleted_at DateTime?
@@id([user_id, event_id])
@@unique([user_id, event_id])
@@map("attendees")
}
保存模式后,我运行npx prisma migrate dev
,它创建了迁移并成功迁移。在postgres中快速浏览一下,可以看到表已经创建,并且dT+
显示新类型和3个条目也已经添加。
然后我注意到,随后的迁移运行开始无缘无故地为与会者表添加一些奇怪的修改表行。我检查了迁移,没有任何原因。下面是与会者表的迁移,您可以看到状态列的定义非常清楚:
-- CreateTable
CREATE TABLE "attendees" (
"user_id" BIGINT NOT NULL,
"event_id" BIGINT NOT NULL,
"status" "ApprovalStatus" NOT NULL DEFAULT 'APPROVED',
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3),
"deleted_at" TIMESTAMP(3),
CONSTRAINT "attendees_pkey" PRIMARY KEY ("user_id","event_id")
);
现在,即使没有对模式中的任何内容进行更改,并且所有先前的迁移都已正确应用,运行npx prisma migrate dev
(带或不带——create-only)将始终生成以下内容的迁移:
/*
Warnings:
- The `status` column on the `attendees` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- AlterTable
ALTER TABLE "attendees" DROP COLUMN "status",
ADD COLUMN "status" "ApprovalStatus" NOT NULL DEFAULT 'APPROVED';
它的行为就好像列的类型或名称发生了变化,即使模型甚至整个模式都没有发生变化。如果我多次运行generate命令,它每次都会用完全相同的内容创建相同的迁移。我认为它可能与迁移顺序有关,但除非它随机进行迁移,否则ApprovalStatus迁移会先于参与者进行。我真的看不出它有什么理由这样做,但我不确定该如何进行。欢迎提出任何建议。
编辑:附加信息
"prisma"^ 4.6.0"
"express"^ 4.17.2"
"typescript"^ 4.8.4"
psql (15.0, server 12.13 (Debian 12.13-1.pgdg110+1))
在Prisma v4.6.0中引入了一个回归,在这个问题中可以看到Prisma删除并重新创建枚举字段。这个问题在Prisma v4.6.1中得到了修复。请更新到最新版本,您应该不会在Prisma迁移中遇到此问题。