Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

647
Views
Update parent and upsert child in Prisma

I have a parent-child relation like below:

model User {
  id               Int                @id @default(autoincrement())
  authId           String             @unique @default("N/A") @map("auth_id") @db.VarChar(128)
  email            String             @unique @db.VarChar(256)
  firstName        String             @map("first_name") @db.VarChar(64)
  lastName         String             @map("last_name") @db.VarChar(64)
  profile          Profile?
}

model Gender {
  id      Int       @id @default(autoincrement())
  text    String
  Profile Profile[]
}
model MedicalCondition {
  id       Int       @id @default(autoincrement())
  text     String
  Profiles Profile[]
}

model Profile {
  id                Int                @id @default(autoincrement())
  dob               String             @db.VarChar(32)
  weightLbs         Float              @map("weight_lbs")
  heightIn          Int                @map("height_in") @db.SmallInt
  user              User               @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId            Int                @unique @map("user_id")
  gender            Gender             @relation(fields: [genderId], references: [id])
  genderId          Int                @map("gender_id")
  lifeStyle         LifeStyle?         @relation(fields: [lifeStyleId],              @map("life_style_id")
  medicalConditions MedicalCondition[]
}

I'm quite new to node.js and Prisma and I'm wondering is it possible to update User and upsert Profile together like below?

const updateWithProfile = (where: IUserUniqueInput, data: IUserUpdate, medicalConditions?: IMedicalConditionWhereUniqueInput[]): Promise<IUser> => {
  return user.update({
    where,
    data:{
      ...data,
      profile: {
        upsert:{
          ????
        },
      }
    }
  });
};
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here is my working solution for upsert one-to-one and many-to-many relations:

const update = async (
  data: IProfileUpdate,
  medicalConditions?: IMedicalConditionWhereUniqueInput[]
): Promise<IProfile> => {
  return profile.upsert({
    where: {
      userId: data.userId,
    },
    update: {
      ...data,
      // set removes previous values in mapping table and connect inserts new IDs
      medicalConditions: { set: [], connect: medicalConditions }
    },
    create: {
      ...data,
      medicalConditions: { connect: medicalConditions }
    }
  });
};
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!