Prisma.js: 我们发现无法执行的更改

Prisma.js: We found changes that cannot be executed

我在我的项目中使用 prisma.js 作为 ORM。

执行npx prisma migrate dev --name rename_and_add_some_columns后, 我收到此错误:

We found changes that cannot be executed

错误详情:

Step 1 Added the required column CategoryId to the Post table without a default value. There are 2 rows in this table, it is not possible to execute this step. • Step 1 Added the required column ModifiedDate to the Post table without a default value. There are 2 rows in this table, it is not possible to execute this step. • Step 2 Added the required column ModifiedDate to the Profile table without a default value. There are 1 rows in this table, it is not possible to execute this step. • Step 4 Added the required column ModifiedDate to the User table without a default value. There are 2 rows in this table, it is not possible to execute this step.

You can use prisma migrate dev --create-only to create the migration file, and manually modify it to address the underlying issue(s). Then run prisma migrate dev to apply it and verify it works.

我该如何解决?

// 这是我的 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")
}

为了运行此迁移,您需要:

  1. 首先创建可选字段,然后 运行 migrate

  2. 首先用所需的日期填写字段。

  3. 从字段中删除可选的 (?)。

Prisma 会自动添加 @updatedAt(它不是在数据库级别完成的)因此需要遵循这些步骤。

在 Post 模型中,我将 Category 更改为 Category?,将 Int 更改为 Int?

此外,我将修改日期中的 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")
}

或者,您可以将 @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[]
}

我追溯添加了 createdAtupdatedAt 字段,我不想为现有字段提供 updatedAt 值,但我可以接受 now() 将成为现有值的默认值 createdAt

updatedAt 字段的 DateTime? 后面添加一个 ? 问号,使其成为可选的。当您迁移架构时,该字段默认为 null,但会在后续更新行时按预期填充。

model MyModel {
...
    createdAt   DateTime  @default(now())
    updatedAt   DateTime? @updatedAt
}