NodeJS js:我们发现了无法执行的更改

9wbgstp7  于 2023-02-03  发布在  Node.js
关注(0)|答案(5)|浏览(162)

我在我的项目中使用prisma.js作为ORM。
在执行npx prisma migrate dev --name rename_and_add_some_columns之后,我得到了这个错误:
我们发现无法执行的更改
错误详情:
步骤1在Post表中添加了所需的列CategoryId,没有默认值。该表中有2行,无法执行此步骤。·步骤1在Post表中添加了所需的列ModifiedDate,没有默认值。该表中有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")
}
ou6hu8tu

ou6hu8tu1#

要运行此迁移,您需要:
1.首先创建可选字段,然后运行migrate
1.请先在字段中填写所需日期。
1.从字段中删除可选的(?)。
Prisma会自动添加@updatedAt(不是在数据库级别完成的),因此需要遵循以下步骤。

erhoui1w

erhoui1w2#

或者,你可以只把@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[]
}
krcsximq

krcsximq3#

在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")
}
rwqw0loc

rwqw0loc4#

我追溯性地添加了createdAtupdatedAt字段,并且我不想为现有字段提供updatedAt值,但是我可以接受now()将成为现有值的默认createdAt这一事实。
updatedAt字段的DateTime?后面添加一个?问号,使其成为可选字段。移植方案时,默认情况下该字段为null,但在后续更新该行时将按预期填充。

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

rqqzpn5f5#

您必须重置数据库和模式
1.获取数据库的备份(注意,如果更改某些数据类型,则必须更改数据!)
1.重置prisma迁移:
npx棱镜迁移重置
1.将模式迁移到数据库:
npx棱镜迁移开发

相关问题