不能用Prisma, Postgresql和Nestjs创建项目



我正在建立一个服装市场,但我得到一个错误,当我试图创建一个服装项目。下面是createItem Service

中的错误
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,
},
});
}

我试图解构和添加,然后添加到创建,但它会导致更多的错误,如预期的类型来自属性'类别',这是在这里声明的类型'(Without<ItemCreateInput,>,ItemUncheckedCreateInput) | (Without<…>,ItemCreateInput)"。

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

我们不这样写:

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;
}

相关内容

  • 没有找到相关文章

最新更新