简体   繁体   English

如何更新 Nestjs 中具有“一对多”关系的 Prisma model

[英]How to update a Prisma model that has a relation "one-to-many" in Nestjs

I have this two prisma models in nestjs:我在 nestjs 中有这两个 prisma 模型:

model DevInformations {
  userId      Int      @unique
  relatedUser User     @relation(fields: [userId], references: [id])
  name        String
  lastName    String
  dateOfBirth DateTime
  telNumber   String
  city        String
  zip         String
  country     String
  resume      String?
  links       Links[]
}

and

model Links {
  id          Int             @id @unique @default(autoincrement())
  userId      Int
  relatedUser DevInformations @relation(fields: [userId], references: [userId])
  name        String
  link        String
}

I have no problem with the create method but I cannot find a way to update the links field without typescript yelling at me or having nestjs errors.我对 create 方法没有问题,但我找不到一种方法来更新链接字段,而不会 typescript 对我大喊大叫或出现 nestjs 错误。

I don't understand the steps to update this specific field.我不明白更新此特定字段的步骤。

For example here is my code例如这里是我的代码

const devInfo = await this.prisma.devInformations.update({
      where: {
        userId,
      },
      data: {
        ...rest,
        links: {
          create: [...dto.links],
        },
      },
      include: {
        links: true,
      },
    });

And my DTO:还有我的 DTO:

export class UpdateDevInfosDTO {
  @IsNumber()
  @IsNotEmpty()
  userId: number;

  ....All the fields,

  @IsArray()
  links: ILinkInput[];
}

export interface ILinkInput {
  id: number;
  name: string;
  link: string;
}

What kind of errors are you getting?你得到什么样的错误? I managed to replicate the model with no errors, but without context of what exactly you are trying to do with links, there is little help.我成功地复制了 model,没有出现任何错误,但是如果没有上下文说明您究竟要对链接做什么,那么帮助不大。

* EDIT: I see you are trying to change links, but your code specifically tries to change DevInformations . *编辑:我看到您正在尝试更改链接,但您的代码专门尝试更改DevInformations

Try to change links, but specify related DevInformations instead.尝试更改链接,但改为指定相关的 DevInformations。

Wrote this code that somewhat resembles what you're trying to do.编写的代码有点类似于您要执行的操作。

// Find existing links that you want to update
const existingLinks = await this.prisma.links.findMany({
        where: {
          name: { // Can change to look for devInformations instead
            in: links.map((link) => link.name),
          },
        },
      });
    
// Map their names
const existingLinkNames = existingLinks.map((link) => link.name);
const linksToCreate = links.filter(
        (link) => !existingLinkNames.includes(link.name)
      );
const linksToUpdate = links.filter((hub) =>
        existingLinkNames.includes(link.name)
      );
const createdLinks = await this.prisma.links.createMany({
        data: linksToCreate.map((link) => ({
          name: link.name,
          // Connect with devInformations
          connect: {
              userId: devInfo.userId
          }
        })),
      });

// Perform same operations to match your use case
const updatedLinks = await this.prisma.links.updateMany({
        where: {
          name: {
            in: linksToUpdate.map((link) => link.name),
          },
        },
        data: {
          description: {
            set: linksToUpdate.map((link) => link.description),
          },
        },
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM