我在我的项目中使用了 prisma.js 作为 ORM。
执行后npx prisma migrate dev --name rename_and_add_some_columns
,我得到了这个错误:
我们发现无法执行的更改
错误详情:
步骤 1 将所需列添加
CategoryId
到Post
表中,但没有默认值。此表有 2 行,无法执行此步骤。• 步骤1 将所需列添加ModifiedDate
到Post
表中,但没有默认值。此表有 2 行,无法执行此步骤。• 步骤2 将所需列添加ModifiedDate
到Profile
表中,但没有默认值。此表有 1 行,无法执行此步骤。• 步骤4 将所需列添加ModifiedDate
到User
表中,但没有默认值。此表有 2 行,无法执行此步骤。您可以使用 prisma migrate dev --create-only 创建迁移文件,并手动修改它以解决潜在问题。然后运行 prisma migrate dev 来应用它并验证它是否有效。
我该如何解决?
// 这是我的 Prisma 模式文件,
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Category {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Title String @db.VarChar(50)
IsActive Boolean
Posts Post[]
}
model Post {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Title String @db.VarChar(255)
Description String?
IsPublished Boolean @default(false)
IsActive Boolean @default(true)
IsActiveNewComment Boolean @default(true)
Author User @relation(fields: [AuthorId], references: [Id])
AuthorId Int
Comment Comment[]
Tag Tag[] @relation("TagToPost", fields: [tagId], references: [Id])
tagId Int?
Category Category @relation(fields: [CategoryId], references: [Id])
CategoryId Int
}
model User {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Email String @unique
Name String?
Posts Post[]
Profile Profile?
Comments Comment[]
}
model Profile {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Bio String?
User User @relation(fields: [UserId], references: [Id])
UserId Int @unique
}
model Comment {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Comment String
WrittenBy User @relation(fields: [WrittenById], references: [Id])
WrittenById Int
Post Post @relation(fields: [PostId], references: [Id])
PostId Int
}
model Tag {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @updatedAt
Title String @unique
Posts Post[] @relation("TagToPost")
}