Using upsert when `Notifications` table doesn't exist yet for user

I have a query to update a users notification preferences, but there's no data existing data yet inside the table. How can I change the query to accodomate that?
await prisma.notifications.upsert({
where: { id },
data: {
type,
communication,
marketing,
social,
security,
mobile,
},
});
await prisma.notifications.upsert({
where: { id },
data: {
type,
communication,
marketing,
social,
security,
mobile,
},
});
I assume upsert is what needs to be used? For reference here is my Notifications model
model Notifications {
id String @id @default(cuid())
userId String
type NotificationType @default(none)
mobile Boolean
communication Boolean
social Boolean
marketing Boolean
security Boolean
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model Notifications {
id String @id @default(cuid())
userId String
type NotificationType @default(none)
mobile Boolean
communication Boolean
social Boolean
marketing Boolean
security Boolean
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
1 Reply
iPheNoMeNaL-oG
iPheNoMeNaL-oG2mo ago
It seems this is the way to create if it doesn't exist yet
await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
id,
userId,
type,
communication,
marketing,
social,
security,
mobile,
},
});
await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
id,
userId,
type,
communication,
marketing,
social,
security,
mobile,
},
});
but userId is not defined in create
Want results from more Discord servers?
Add your server