0

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

执行后npx prisma migrate dev --name rename_and_add_some_columns,我得到了这个错误:

我们发现无法执行的更改

错误详情:

步骤 1 将所需列添加CategoryIdPost表中,但没有默认值。此表有 2 行,无法执行此步骤。• 步骤1 将所需列添加 ModifiedDatePost表中,但没有默认值。此表有 2 行,无法执行此步骤。• 步骤2 将所需列添加ModifiedDateProfile表中,但没有默认值。此表有 1 行,无法执行此步骤。• 步骤4 将所需列添加 ModifiedDateUser表中,但没有默认值。此表有 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")
}
4

3 回答 3

2

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

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

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

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

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

于 2021-06-07T09:12:57.447 回答
1

或者,您可以只添加@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[]
}
于 2021-06-24T19:38:00.247 回答
0

在 Post 模型中,我将CategorytoCategory?InttoInt?

另外,我将DatetimeModifiedDate 更改为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")
}
于 2021-06-07T09:28:54.637 回答