简体   繁体   中英

Prisma update data with relation and where-clause

I want to update data with a relation to other tables, but I don't really find a solution for that. I'm using Prisma in a NodeJS-Application. I give the id (Number(req.params.id)) in the URL.

For example: http://localhost:3000/api/apprentice/42

schema.prisma:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model apprentice {
  apId          Int         @id @default(autoincrement())
  geId          Int         @db.TinyInt
  apFirstname   String      @db.VarChar(50)
  apLastname    String      @db.VarChar(50)
  apBirthdate   DateTime?   @db.Date
  apPhoneNumber String      @db.VarChar(12)
  gender        gender      @relation(fields: [geId], references: [geId], onDelete: NoAction, onUpdate: NoAction, map: "fk_apprentice_gender1")
  execution     execution[]

  @@index([geId], map: "fk_apprentice_gender1_idx")
}

model execution {
  exId       Int        @id @default(autoincrement())
  reId       Int        @db.TinyInt
  apId       Int
  exDate     DateTime   @db.DateTime(0)
  exMessage  String     @db.VarChar(8000)
  apprentice apprentice @relation(fields: [apId], references: [apId], onDelete: Cascade, onUpdate: NoAction, map: "execution_ibfk_1")
  region     region     @relation(fields: [reId], references: [reId], onDelete: NoAction, onUpdate: NoAction, map: "fk_execution_region1")

  @@index([apId], map: "execution_ibfk_1")
  @@index([reId], map: "fk_execution_region1_idx")
}

model gender {
  geId       Int          @id @db.TinyInt
  gender     String       @db.VarChar(6)
  apprentice apprentice[]
}

model message {
  meId      Int    @id @default(autoincrement()) @db.TinyInt
  reId      Int    @db.TinyInt
  meMessage String @db.VarChar(8000)
  region    region @relation(fields: [reId], references: [reId], onDelete: NoAction, onUpdate: NoAction, map: "fk_message_region1")

  @@index([reId], map: "fk_message_region1_idx")
}

model region {
  reId      Int         @id @db.TinyInt
  reOrt     String      @db.VarChar(50)
  execution execution[]
  message   message[]
}

Request in server.js (NodeJS):

app.put("/api/apprentice/:id", async (req, res) => {
  const updatedApprentice = await prisma.apprentice.update({
    where: {
      apId: Number(req.params.id)
    },
    data: {
      gender: {
        update: {
          geId: req.body.apprentice.gender,
        }
      },
      apFirstname: req.body.apprentice.firstName,
      apLastname: req.body.apprentice.lastName,
      apBirthdate: new Date(req.body.apprentice.birthdate),
      apPhoneNumber: req.body.apprentice.phoneNumber,
      execution: {
        update: {
          exDate: req.body.execution.datetime,
          exMessage: req.body.execution.message,
          reId: req.body.region
        }
      }
    }
  });
  res.send(updatedApprentice);
});

Request body:

{
    "execution": {
        "datetime": "2022-06-12 10:30:00",
        "message": "test for 3 und Person name1 name2 am 2022-06-14 um 07:00:00"
    },
    "apprentice": {
        "firstName": "name1",
        "lastName": "name2",
        "birthdate": "2005-06-29",
        "gender": 2,
        "phoneNumber": "+41111111111"
    },
    "region": 1
}

MySQL-Schema 在此处输入图像描述

Here you need two queries, the first one being to find the latest execution row:

const latestExId = (
  await prisma.execution.findFirst({
    where: {
      apId: Number(req.params.id),
    },
    orderBy: { exDate: "desc" },
    select: { exId: true },
  })
)?.exId;
if (!latestExId) {
  throw new Error("This apprentice has no executions ongoing");
}

const updatedApprentice = await prisma.apprentice.update({
  where: {
    apId: Number(req.params.id),
  },
  data: {
    geId: req.body.apprentice.gender,
    apFirstname: req.body.apprentice.firstName,
    apLastname: req.body.apprentice.lastName,
    apBirthdate: new Date(req.body.apprentice.birthdate),
    apPhoneNumber: req.body.apprentice.phoneNumber,
    execution: {
      update: {
        data: {
          exDate: req.body.execution.datetime,
          exMessage: req.body.execution.message,
          reId: req.body.region,
        },
        where: {
          exId: latestExId,
        },
      },
    },
  },
});

(I have not tested this code)

It also seems to me that you meant to update the geId field of the apprentice, rather than to update the geId of the gender table. Note that you could use an enum or simply a string field for the apprentice's gender value instead of having a specific table for gender.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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