generator client {
provider = "prisma-client-js"
output = "generated/client"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
name String
lastName String
email String @unique
linkedEmail String @unique
password String
phone String
birthdate String
gender String
questions Question[]
likedQuestions Question[] @relation("LikedQuestions")
@@map("user")
}
model Question {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
themeText String @map("theme_text")
text String? @default("")
category String
subcategory String
likes Int? @default(0)
likedBy User[] @relation("LikedQuestions")
isLeader Boolean? @default(false) @map("is_leader")
user User? @relation(fields: [userId], references: [id])
userId String?
@@map("question")
}
допустим я создал 1 Question и в нем добавил 1 лайк:
@UsePipes(new ValidationPipe())
@HttpCode(200)
@Auth()
@Put(':id')
async update(
@Body() dto: QuestionDto,
@CurrentUser('id') userId: string,
@Param('id') id: string
) {
return this.questionService.update(dto, id, userId);
}
@UsePipes(new ValidationPipe())
@Patch('like/:id')
@Auth()
async addLike(@Param('id') id: string, @CurrentUser('id') userId: string) {
return this.questionService.addLike(id, userId);
}
async create(dto: QuestionDto, userId: string) {
return this.prisma.question.create({
data: {
...dto,
user: {
connect: {
id: userId,
},
},
},
});
}
async addLike(questionId: string, userId: string) {
return this.prisma.question.update({
where: { id: questionId },
data: {
likes: { increment: 1 },
likedBy: {
connect: {
id: userId,
},
}
}
});
}
И потом я удалил сам question и вот эти "A,B" почему-то остались в LikedQuestions, что я не так связал?