【问题标题】:Can not create an item with Prisma, Postgresql and Nestjs无法使用 Prisma、Postgresql 和 Nestjs 创建项目
【发布时间】:2022-12-21 18:28:10
【问题描述】:

我正在建立一个服装市场,但是当我尝试创建一个服装项目时出现错误。这是 createItem 服务中的错误

Types of property 'userId' are incompatible.
Type 'number' is not assignable to type 'never'.

这是我在 Prisma 中的模型

model User {
  id       Int       @id @default(autoincrement())
  email    String    @unique
  fullName String
  password String
  items    Item[]
  location Location?
  phone    String?
  image    String?
}

model Location {
  id     Int    @id @default(autoincrement())
  name   String
  user   User   @relation(fields: [userId], references: [id])
  userId Int    @unique
}

model Item {
  id        Int       @id @default(autoincrement())
  user      User      @relation(fields: [userId], references: [id])
  userId    Int
  style     Style?
  images    String[]
  price     Int
  size      Size?
  category  Category?
  brand     Brand?
  colour    Colour?
  condition Int
}

model Size {
  id     Int    @id @default(autoincrement())
  name   String @unique
  item   Item?  @relation(fields: [itemId], references: [id])
  itemId Int?   @unique
}

model Colour {
  id     Int    @id @default(autoincrement())
  name   String @unique
  item   Item?  @relation(fields: [itemId], references: [id])
  itemId Int?   @unique
}

model Category {
  id     Int    @id @default(autoincrement())
  name   String @unique
  item   Item?  @relation(fields: [itemId], references: [id])
  itemId Int?   @unique
}

model Style {
  id     Int    @id @default(autoincrement())
  name   String @unique
  item   Item?  @relation(fields: [itemId], references: [id])
  itemId Int?   @unique
}

model Brand {
  id     Int    @id @default(autoincrement())
  name   String @unique
  item   Item?  @relation(fields: [itemId], references: [id])
  itemId Int?   @unique
}

这是我的项目创建 DTO

export class CreateItemDto {
  @IsNotEmpty()
  style: string; // I should relate to style model (Vintage, Modern etc.)

  @IsNotEmpty()
  images: string[]; // Urls of the images

  @IsNotEmpty()
  @IsNumber()
  price: number;

  @IsNotEmpty()
  @IsString()
  size: string; // I should relate to size model (S,M,XL etc)

  @IsString()
  @IsNotEmpty()
  category: string;  

  @IsNotEmpty()
  @IsString()
  brand: string;

  @IsNotEmpty()
  @IsString()
  colour: string;

  @IsNotEmpty()
  @IsNumber()
  condition: number;
}

这是我在服务中的 createItem 函数。

async createItem(dto: CreateItemDto, userId: number) {
    return await this.prisma.item.create({
      data: {
        userId,
        ...dto,
      },
    });
  }

我试图对 dto 进行 desrutize,然后添加到创建中,但它会导致更多错误,例如 The expected type comes from property 'category' which is declared here on type '(Without<ItemCreateInput, ItemUncheckedCreateInput> & ItemUncheckedCreateInput) | (没有<...> & ItemCreateInput)'。

【问题讨论】:

  • 您能否显示调用createItem() 的代码以及传递给createItem 函数的参数?

标签: typescript postgresql nestjs prisma


【解决方案1】:

问题是将模型与关系联系起来。

所以不要让它像这样:

async createItem(dto: CreateItemDto, userId: number) {
    return await this.prisma.item.create({
      data: {
        userId,
        ...dto,
      },
    });
  }

我需要这样做:

    async createItem(dto: CreateItemDto, userId: number) {
    const item = await this.prisma.item.create({
      data: {
        user: { connect: { id: userId } }, // Connect a records with realations
        condition: dto.condition,
        price: dto.price,
        description: dto.description,
        brand: { connect: { value: dto.brand } },
        category: { connect: { id: dto.categoryId } },
        colour: { connect: { value: dto.colour } },
        images: dto.images,
        size: dto.size,
        style: { connect: { value: dto.style } },
        gender: dto.gender,
      },
      select: {
        id: true,
        brand: true,
        category: true,
        price: true,
      },
    });

    return item;
  }

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2021-09-13
    • 2022-01-04
    • 2021-11-27
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2020-05-09
    相关资源
    最近更新 更多