[英]UPDATE IGNORE with function LOWER() in WHERE-clause does nothing
[英]Prisma update data with relation and where-clause
我想更新與其他表相關的數據,但我並沒有真正找到解決方案。 我在 NodeJS 應用程序中使用 Prisma。 我在 URL 中給出了 id (Number(req.params.id))。
例如:http://localhost:3000/api/apprentice/42
架構.棱鏡:
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[]
}
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);
});
請求正文:
{
"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
}
這里需要兩個查詢,第一個是查找最新的執行行:
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,
},
},
},
},
});
(我沒有測試過這段代碼)
在我看來,您還打算更新學徒的geId
字段,而不是更新性別表的geId
。 請注意,您可以使用枚舉或簡單的字符串字段作為學徒的性別值,而不是使用特定的性別表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.