I have a Notifications
model in my primsa.schema
model Notifications {
id String @id @default(cuid())
userId String //@unique ???? does this attribute need to be added
type NotificationType @default(none)
mobile Boolean
communication Boolean
social Boolean
marketing Boolean
security Boolean
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
I want to make sure the relation to User
is what it should be
model User {
id String @id @default(cuid())
name String?
accounts Account[]
notifications Notifications[]
}
I’m then attempting to use upsert
to either create a new record if it does not exist or update the existing one inside of a server action. id
is passed to the function via session data of the user
The data for notifications is being passed from my form like this
const update = await updateNotifications(
session.user.id,
data.type,
data.communication_emails,
data.marketing_emails,
data.social_emails,
data.security_emails,
data.mobile,
);
To the server action below.
async function updateNotifications(
id: string,
type: NotificationType,
communication: boolean,
marketing: boolean,
social: boolean,
security: boolean,
mobile: boolean,
) {
"use server";
try {
await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
id,
userId,
type,
communication,
marketing,
social,
security,
mobile,
},
});
return upsertNotifications;
} catch (error) {
console.error("Error updating user: ", error);
}
}
I’m having issues with the userId
that no value exists. So that brings me back to the schema and if userId
needs to be unique and how does that need to be passed back to the function where I’m using upsert
?
Update
I was able to successfully create notifications
async function updateNotifications(
id: string,
type: NotificationType,
communication: boolean,
marketing: boolean,
social: boolean,
security: boolean,
mobile: boolean,
) {
"use server";
try {
const update = await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
userId: id,
type,
communication,
marketing,
social,
security,
mobile,
},
});
But it is still creating a new notification
object every time instead of updating the existing one
upsert
needs a where
clause so it could find entries that already exist and update them. You have it, but I think the problem is that you pass session.user.id
(so the user id?) instead of notification.id
. So you are trying to find existing notification
by session.user.id
, it fails to find anything, and creates a new notification with auto generated id (in your schema => @id @default(cuid())
) which has no correlation to the user id.
So to fix it you need to somehow pass notification id here:
const update = await updateNotifications(
// session.user.id <- remove this
notificationIdToUpdate,
data.type,
data.communication_emails,
data.marketing_emails,
data.social_emails,
data.security_emails,
data.mobile,
);
2