Updating or creating table in Prisma using upsert

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật