nest prisma 错误:类型“number”不可分配给类型“never”

问题描述 投票:0回答:1

我将 prisma 与 Nestjs 一起使用。

方法是: enter image description here

错误是:

Type 'CreateFilmDto' is not assignable to type '(Without<FilmCreateInput, FilmUncheckedCreateInput> & FilmUncheckedCreateInput) | (Without<...> & FilmCreateInput)'.
  Type 'CreateFilmDto' is not assignable to type 'Without<FilmUncheckedCreateInput, FilmCreateInput> & FilmCreateInput'.
    Type 'CreateFilmDto' is not assignable to type 'Without<FilmUncheckedCreateInput, FilmCreateInput>'.
      Types of property 'categoryId' are incompatible.
        Type 'number' is not assignable to type 'never'.

胶片型号为:

model Film {
  id             Int              @id @default(autoincrement())
  title          String           @db.VarChar(255)
  thumbnail      String           @db.VarChar(255)
  description    String           @db.VarChar(1000)
  rate           Decimal          @db.Decimal(2, 1)
  minAge         Int
  year           Int
  duration       Int
  isSubsRequired Boolean          @default(false)
  categoryId     Int
  category       Category         @relation(fields: [categoryId], references: [id])
  genres         Film_Genres[]
  countries      Film_Countries[]
  actors         Film_Actors[]
  comments       Comment[]
  favourites     Favourite[]

  @@map("films")
}

类别型号为:

model Category {
  id               Int        @id @default(autoincrement())
  name             String     @db.VarChar(255)
  parentCategoryId Int?
  parentCategory   Category?  @relation("parentCategory", fields: [parentCategoryId], references: [id])
  subCategories    Category[] @relation("parentCategory")
  films            Film[]
  tv               TV[]

  @@map("categories")
}

CreateFilmDto 是:

import {
  IsString,
  IsNotEmpty,
  IsNumber,
  IsBoolean,
  IsPositive,
  Validate,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { IsValidCategoryId } from '../../validators';

export class CreateFilmDto {
  @ApiProperty({
    description: 'The title of the film',
    example: 'Inception',
  })
  @IsNotEmpty()
  @IsString()
  title: string;

  @ApiProperty({
    description: 'The thumbnail URL of the film',
    example: 'https://example.com/thumbnail.jpg',
  })
  @IsNotEmpty()
  @IsString()
  thumbnail: string;

  @ApiProperty({
    description: 'The description of the film',
    example: 'A science fiction thriller.',
  })
  @IsNotEmpty()
  @IsString()
  description: string;

  @ApiProperty({
    description: 'The rate or rating of the film',
    example: 4.5,
  })
  @IsNumber()
  @IsPositive()
  rate: number;

  @ApiProperty({
    description: 'The release year of the film',
    example: 2010,
  })
  @IsNumber()
  @IsPositive()
  year: number;

  @ApiProperty({
    description: 'The duration of the film in minutes',
    example: 148,
  })
  @IsNumber()
  @IsPositive()
  duration: number;

  @ApiProperty({
    description: 'Indicates if subtitles are required',
    example: true,
  })
  @IsBoolean()
  isSubsRequired: boolean;

  @ApiProperty({
    description: 'The ID of the film category',
    example: 1,
  })
  @IsNumber()
  @IsPositive()
  categoryId: number;
}

其他 15 个型号工作正常,但这个型号出现错误。有谁可以帮忙解决一下问题吗?

我已经检查过所有地方的 typeofcategoryId 都是相同的 我没有任何想法

prisma nest
1个回答
0
投票

我在 Prisma 就遇到过这种情况。这是一个转移注意力的事情,因为 Prisma 类型检查有点缺乏,实际问题是

createFilmDto
中缺少其他一些必需的属性。您可以仔细检查是否存在所有必需的属性,或者可以用显式外键关系替换代码(这将揭示实际的类型错误)。

例如 之前:

prisma.film.create({
 data:{
  categoryId: categoryId
 }
})

之后:

prisma.film.create({
 data:{
  category:{
    connect: {
      id: categoryId
    }
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.