在 Prisma 中将数据插入隐式多对多关系

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

我正在建立一个类似于 LinkedIn 的网站/确实,当用户申请工作时尝试更新工作行时遇到困难。我使用 Nest.js、Postgres 和 Prisma 作为 ORM。

以下是用户和作业模型的架构:

model Job {
  id                 String          @id @default(cuid())
  title              String
  description        String
  postedAt           DateTime        @default(now())
  location           Location
  experienceLevel    ExperienceLevel
  monthlySalary      Int
  currency           String
  requirements       Requirement[]
  activelyRecruiting Boolean         @default(true)
  companyId          String
  company            Company         @relation(fields: [companyId], references: [id], onDelete: Cascade)
  applicants         User[]
}

model User {
  id            String       @id @default(cuid())
  username      String
  password      String
  email         String       @unique
  github        String?
  linkedin      String?
  website       String?
  resume        String?
  experiences   Experience[]
  skills        Skill[]
  education     Education[]
  appliedToJobs Job[]
  createdAt     DateTime     @default(now())
  updatedAt     DateTime     @updatedAt
}

我使用隐式多对多关系,因为我不需要存储有关该关系的元数据。这是我正在编写的用于将用户添加到 Postgres 数据库的代码:

async applyToJob(id: string, accessor: Payload) {
        const user = await this.prisma.user.findUnique({ where: { id: accessor.id } });
        const job = await this.prisma.job.update({ where: { id }, data: { applicants: { create: [user] } } });
        return job;
    }

我也尝试过这个:

async applyToJob(id: string, accessor: Payload) {
        const user = await this.prisma.user.findUnique({ where: { id: accessor.id } });
        const job = await this.prisma.job.update({ where: { id }, data: { applicants: { create: user } } });
        return job;
    }

每当我尝试将用户添加到申请人列表中(然后该列表应该能够更新用户行中的 AppliedToJobs 字段)时,我都会收到以下错误:

PrismaClientKnownRequestError: 
Invalid `this.prisma.job.update()` invocation in

  33
  34 async applyToJob(id: string, accessor: Payload) {
  35     const user = await this.prisma.user.findUnique({ where: { id: accessor.id } });
→ 36     const job = await this.prisma.job.update(
Unique constraint failed on the fields: (`id`)
    at Hr.handleRequestError (C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected][email protected]\node_modules\@prisma\client\runtime\library.js:122:6999)
    at Hr.handleAndLogRequestError (C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected][email protected]\node_modules\@prisma\client\runtime\library.js:122:6388)
    at Hr.request (C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected][email protected]\node_modules\@prisma\client\runtime\library.js:122:6108)
    at l (C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected][email protected]\node_modules\@prisma\client\runtime\library.js:126:10298)
    at JobService.applyToJob (C:\Users\Muham\Documents\dev-jobs\api\src\job\job.service.ts:36:21)
    at C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected][email protected][email protected]\node_modules\@nestjs\core\router\router-execution-context.js:46:28
    at C:\Users\Muham\Documents\dev-jobs\api\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected][email protected]_r[email protected]\node_modules\@nestjs\core\router\router-proxy.js:9:17

我尝试在网上寻找解决方案,但大多数人说一定存在具有相同ID的冲突字段,这实际上是不可能的,因为ID是由Postgres随机生成的。如果有人可以告诉我如何将用户连续推送到申请人字段,以便以前的申请人保留在列表中,这将非常有帮助。

postgresql nestjs many-to-many prisma unique-constraint
1个回答
0
投票

您正在做的是尝试创建新用户,而不是将现有用户关联为工作申请人。应该是这样的

async applyToJob(id: string, accessor: Payload) {
    const user = await this.prisma.user.findUnique({ where: { id: accessor.id } });
    const job = await this.prisma.job.update({ where: { id }, data: { applicants: { update: [{where: {id: user.id}, data: user}] } } });
    return job;
}
© www.soinside.com 2019 - 2024. All rights reserved.