我在我的项目中使用了 prisma.js 作为 ORM。
执行
npx prisma migrate dev --name rename_and_add_some_columns
后,
我收到此错误:
我们发现无法执行的更改
错误详情:
步骤 1 将所需列
添加到CategoryId
表中 没有默认值。该表中有2行,不是 可以执行此步骤。 • 步骤1 添加所需的列Post
到ModifiedDate
表,没有默认值。有 该表中有2行,无法执行此步骤。 • 步骤 2 将所需列Post
添加到ModifiedDate
表中 没有默认值。该表中有 1 行,不是 可以执行此步骤。 • 步骤 4 添加所需列Profile
到ModifiedDate
表,没有默认值。有 此表中有 2 行,无法执行此步骤。User
您可以使用 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")
}
为了运行此迁移,您需要:
首先创建可选字段,然后运行
migrate
首先填写所需日期的字段。
从字段中删除可选 (
?
)。
Prisma 自动添加
@updatedAt
(不是在数据库级别完成),因此需要遵循这些步骤。
或者,您可以将
@default(now())
添加到您的 ModifiedDate
属性中。因此,例如,Category
模型将是:
model Category {
Id Int @id @default(autoincrement())
CreatedDate DateTime @default(now())
ModifiedDate DateTime @default(now()) @updatedAt
Title String @db.VarChar(50)
IsActive Boolean
Posts Post[]
}
在Post模型中,我将
Category
更改为Category?
,将Int
更改为Int?
另外,我将 ModifiedDate 中的
Datetime
更改为 Datetime?
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")
}
我追溯添加了
createdAt
和 updatedAt
字段,并且我不想为现有字段提供 updatedAt
值,但我可以接受这样一个事实:now()
将成为默认的 createdAt
现有的价值观。
在
?
字段的 DateTime?
后面添加 updatedAt
问号,使其可选。当您迁移架构时,该字段默认为 null
,但会在后续更新行时按预期填充。
model MyModel {
...
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
}
执行两项操作
并按照命令操作后