Tyler Bell
01/04/2022, 4:22 AMmodel App {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
name String
redirects Redirects[]
@@index([slug], map: "App.slug_index")
}
model Redirects {
id String @id @default(cuid())
global Boolean
afterSignup String?
afterLogin String?
afterLogout String?
appId String
app App @relation(fields: [appId], references: [id])
}
I want to update a single record that matches an appId and where global is true If a record doesn’t match that, I would like it to be created.
Having difficulties doing that tho, since the upsert method only accepts the id field in the where argument.
So what’s the best way to do this? The following code is essentially what I’m trying to do.
prisma.redirects.upsert({
where: {
appId,
global:true
},
create: {
...redirects,
app: { connect: { id: appId } },
},
update: {
...redirects,
app: { connect: { id: appId } },
}
})
Thanks!Maciek K
01/04/2022, 7:50 AMconst redirect = await prisma.redirects.findFirst({ where: {appId, global: true });
await prisma.redirects.upsert({
where: {
id: redirect.id
},
create: {
...redirects,
app: { connect: { id: appId } },
},
update: {
...redirects,
app: { connect: { id: appId } },
}
})Maciek K
01/04/2022, 7:52 AMTyler Bell
01/04/2022, 2:12 PM