Next.js Discord

Discord Forum

How can I allow users to add multiple distinct work experiences to a resume in my Prisma db?

Unanswered
Yellow croaker posted this in #help-forum
Open in Discord
Yellow croakerOP
Hello!
My prisma schema looks like this:
model Resume {
  id              String              @id @default(cuid())
  userId          String
  user            User                @relation(fields: [userId], references: [id])
  title           String
  personalInfo    PersonalInfo?
  workexperiences WorkExperience[]
  templateStyle   ResumeTemplateStyle @default(BasicTemplate)
}

model WorkExperience {
  id          String    @id @default(cuid())
  position    String
  company     String
  description String?
  startDate   DateTime
  endDate     DateTime?
  resumeId    String    @unique
  resume      Resume    @relation(fields: [resumeId], references: [id], onDelete: Cascade)
}
...


What i want is that user can add multiple different workExperiences to my database.

I have server action that is called
  const processForm: SubmitHandler<any> = async (data) => {
    const workExperienceData = data.workExperience;
    const resumeId = resume.id;
    const result = await UpdateWorkExperience(resumeId, workExperienceData);
  };


export async function UpdateWorkExperience(
  resumeId: string,
  workExperienceData: any,
) {
  console.log('This is resumeID:', resumeId);
  console.log('WORKEXPERIENCEDATA', workExperienceData);
  const resume = await db.resume.update({
    where: {
      id: resumeId,
    },
    data: {
      workexperiences: {
        create: workExperienceData,
      },
    },
  });
  return resume;
}


The data that is given to UpdateWorkExperience looks like this:

This is resumeID: 123
WORKEXPERIENCEDATA [
  {
    position: '111',
    company: '111',
    startDate: '2023-10-21T18:45:30.123Z',
    endDate: '2023-10-21T18:45:30.123Z',
    description: '1111'
  },
  {
    position: '222',
    company: '222',
    startDate: '2023-10-21T18:45:30.123Z',
    endDate: '2023-10-21T18:45:30.123Z',
    description: '222'
  }
]

33 Replies

Yellow croakerOP
But i get this error when u make a submit
- error PrismaClientKnownRequestError: 
Invalid `prisma.resume.update()` invocation:


Unique constraint failed on the fields: (`resumeId`)
    at async UpdateWorkExperience (./app/actions.ts:17:20)
Yellow croakerOP
I think i need to establish a 1-to-m update function here?
Giant panda
you're not passing resumeId to workExperienceData
@Giant panda you're not passing resumeId to workExperienceData
Yellow croakerOP
I think it got The resumeId. Because on The console.log it shows logs The resumeId?
Giant panda
no, inside the workExperienceData array
the objects are missing the resumeId field, required in your prisma model
@Giant panda the objects are missing the resumeId field, required in your prisma model
Yellow croakerOP
Oh right i need to give this resumeId:
model WorkExperience {
  id          String    @id @default(cuid())
  position    String
  company     String
  description String?
  startDate   DateTime
  endDate     DateTime?
  resumeId    String    @unique // this one?
  resume      Resume    @relation(fields: [resumeId], references: [id], onDelete: Cascade)
}
Giant panda
Yes
Your WorkExperience model requires the resumeId field, and your array has objects that don't contain them
@Giant panda Your WorkExperience model requires the resumeId field, and your array has objects that don't contain them
Yellow croakerOP
Thanks i understand now!


export async function UpdateWorkExperience(dataToSend: _any) {
  console.log('dataToSend', dataToSend);

  const resume = await db.resume.update({
    where: {
      id: dataToSend.resumeId,
    },
    data: {
      workexperiences: {
        create: dataToSend,
      },
    },
  });
  console.log('UPDATEDRESUME', resume);
  return resume;
}


- error PrismaClientValidationError: 
Invalid `prisma.resume.update()` invocation:

{
  where: {
    id: "1234"
  },
  data: {
    workexperiences: {
      create: {
        0: {
          position: "ddddf",
          company: "ddddf",
          startDate: "2023-10-21T18:45:30.123Z",
          endDate: "2023-10-21T18:45:30.123Z",
          description: "fasfasffsf"
        },
        resumeId: "1234",
+       position: String
      }
    }
  }
}

Argument `position` is missing.
    at async UpdateWorkExperience (./app/actions.ts:16:20)


  const processForm: SubmitHandler<any> = async (data) => {

    const dataToSend = {
      ...data.workExperience,
      resumeId: resume.id,
    };
    const result = await UpdateWorkExperience(dataToSend);

    console.log('result', result);
  };



should i destruct the workExperiences somehow? Should i also take account if there are for example two objects in the array
Giant panda
no you misunderstood what I said
First of all, you're trying to create several work experiences at a time
right?
is that why you were sending an array?
Yellow croakerOP
Yeah or 1. There can be 1 to multiple.
Giant panda
a) you should rather use the Prisma discord server than the next one, because this isn't next-specific but rather Prisma specific
b) You should rather use the createMany if you're sending several: https://www.prisma.io/docs/concepts/components/prisma-client/crud#create-multiple-records
@Giant panda b) You should rather use the createMany if you're sending several: https://www.prisma.io/docs/concepts/components/prisma-client/crud#create-multiple-records
Yellow croakerOP
Allright thanks! Il add resumeId to all the workExperience objects, and then use createmany or upsert? If that dont work i go to prisma discord 🙂
Giant panda
createMany should do the job
@Giant panda createMany should do the job
Yellow croakerOP
Not sure what im missing here?

Invalid `prisma.resume.update()` invocation:

{
  where: {
    id: "1234"
  },
  data: {
    workexperiences: {
      createMany: [
        {
          position: "fasf",
          company: "fsa",
          startDate: "2023-10-21T18:45:30.123Z",
          endDate: "2023-10-21T18:45:30.123Z",
          description: "fasfaf",
          resumeId: "1234"
        },
        {
          position: "asfasf",
          company: "asfasf",
          startDate: "2023-10-21T18:45:30.123Z",
          endDate: "2023-10-21T18:45:30.123Z",
          description: "fasfsaf",
          resumeId: "1234"
        },
        {
          position: "fdssdfsfd",
          company: "fdssdf",
          startDate: "2023-10-20T18:45:30.123Z",
          endDate: "2023-10-20T18:45:30.123Z",
          description: "fasfafs",
          resumeId: "1234"
        }
      ]
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    }
  }
}

Argument `createMany`: Invalid value provided. Expected WorkExperienceCreateManyResumeInputEnvelope, provided (Object, Object, Object).
    at async UpdateWorkExperience (./app/actions.ts:16:20)


export async function UpdateWorkExperience(newData: _any) {
  console.log('dataToSend in update', newData[0].resumeId);

  const resume = await db.resume.update({
    where: {
      id: newData[0].resumeId,
    },
    data: {
      workexperiences: {
        createMany: newData,
      },
    },
  });
  console.log('UPDATEDRESUME', resume);
  return resume;
}



  const processForm: SubmitHandler<any> = async (data) => {
    console.log('data', resume.id);
    const newData = data.workExperience.map((item) => {
      return {
        ...item,
        resumeId: resume.id,
      };
    });
    console.log('FormData sent:', newData);
    const result = await UpdateWorkExperience(newData);

    console.log('result', result);
  };
Giant panda
Try:
createMany: {
  data: newData
}
I recommend you use the official Prisma typings for debugging and troubleshooting
@Giant panda Try: createMany: { data: newData }
Yellow croakerOP
  const resume = await db.resume.update({
    where: {
      id: newData[0].resumeId,
    },
    data: {
      workexperiences: {
        createMany: {
          data: newData,
        },
      }, // Add a comma here
    },
  });


Unknown argument `resumeId`. Available options are listed in green.
    at async UpdateWorkExperience (./app/actions.ts:16:20)


Gonna take look on prisma debug now, it seems that it needs some envirmoent settings to be done
Yellow croakerOP
Its a bit strange that the the workExperiences have the resumeId but it still says that resumeId is unknown?
 error PrismaClientValidationError: 
Invalid `prisma.resume.update()` invocation:

{
  where: {
    id: "1234"
  },
  data: {
    workexperiences: {
      createMany: {
        data: [
          {
            position: "ffff",
            company: "fff",
            startDate: "2023-10-21T18:45:30.123Z",
            endDate: "2023-10-21T18:45:30.123Z",
            description: "faf",
            resumeId: "1234"
          },
          {
            position: "fasfasf",
            company: "fasfas",
            startDate: "2023-10-20T18:45:30.123Z",
            endDate: "2023-10-20T18:45:30.123Z",
            description: "fasfaafsaf",
            resumeId: "1234"
          },
          {
            position: "fasf",
            company: "faf",
            startDate: "2023-10-20T18:45:30.123Z",
            endDate: "2023-10-20T18:45:30.123Z",
            description: "fsafasfa",
            resumeId: "1234"
          },
          {
            position: "ff",
            company: "ff",
            startDate: "2023-10-21T18:45:30.123Z",
            endDate: "2023-10-21T18:45:30.123Z",
            description: "fsafa",
            resumeId: "1234"
          }
        ]
      }
    }
  }
}

Unknown argument `resumeId`. Available options are listed in green.
    at async UpdateWorkExperience (./app/actions.ts:17:20)
Giant panda
Try without? Maybe since you're used nested inputs the resumeId is inherited from the root resume table
@Giant panda Try without? Maybe since you're used nested inputs the resumeId is inherited from the root resume table
Yellow croakerOP
But If i dont give The resumeId then its The same as we startted?
Giant panda
the best way to find out is if you console log it 😉
it's important to debug, then ask questions
Yellow croakerOP
Yeah did some logging:

- error PrismaClientValidationError: 
Invalid `prisma.resume.update()` invocation:

{
  where: {
    id: "clo0f7ax1000ck93x82umupkv"
  },
  data: {
    workexperiences: {
      createMany: {
        data: {
          personalInfo: {
            name: "ffasdasd",
            email: "ff@gmai.com",
            phone: "5555555555",
            address: "fasfaffaf"
          },
          workExperience: [
            {
              position: "fasfafsf",
              company: "fasfsa",
              startDate: "2023-10-21T18:45:30.123Z",
              endDate: "2023-10-21T18:45:30.123Z",
              description: "dasdad"
            }
          ],
+         position: String
        }
      }
    }
  }
}

Argument `position` is missing.
    at async UpdateWorkExperience (./app/actions.ts:17:20)


export async function UpdateWorkExperience(
  newData: WorkExperience,
  resumeTest: any,
) {
  console.log('dataToSend in update', newData);
  console.log('resume, resume', resumeTest);
  const resume = await db.resume.update({
    where: {
      id: resumeTest.id,
    },
    data: {
      workexperiences: {
        createMany: {
          data: newData,
        },
      },
    },
  });
  console.log('UPDATEDRESUME', resume);
  return resume;
}



  const processForm: SubmitHandler<any> = async (data) => {
    console.log('data', resume.id);
    const newData = data.workExperience.map((item) => {
      return {
        ...item,
        resumeId: resume.id,
      };
    });
    console.log('FormData sent:', newData);
    const result = await UpdateWorkExperience(data, resume);

    console.log('result', result);
  };

It complains about position?
@Yellow croaker Yeah did some logging: - error PrismaClientValidationError: Invalid `prisma.resume.update()` invocation: { where: { id: "clo0f7ax1000ck93x82umupkv" }, data: { workexperiences: { createMany: { data: { personalInfo: { name: "ffasdasd", email: "ff@gmai.com", phone: "5555555555", address: "fasfaffaf" }, workExperience: [ { position: "fasfafsf", company: "fasfsa", startDate: "2023-10-21T18:45:30.123Z", endDate: "2023-10-21T18:45:30.123Z", description: "dasdad" } ], + position: String } } } } } Argument `position` is missing. at async UpdateWorkExperience (./app/actions.ts:17:20) js export async function UpdateWorkExperience( newData: WorkExperience, resumeTest: any, ) { console.log('dataToSend in update', newData); console.log('resume, resume', resumeTest); const resume = await db.resume.update({ where: { id: resumeTest.id, }, data: { workexperiences: { createMany: { data: newData, }, }, }, }); console.log('UPDATEDRESUME', resume); return resume; } js const processForm: SubmitHandler<any> = async (data) => { console.log('data', resume.id); const newData = data.workExperience.map((item) => { return { ...item, resumeId: resume.id, }; }); console.log('FormData sent:', newData); const result = await UpdateWorkExperience(data, resume); console.log('result', result); }; It complains about position?
Giant panda
why is personal info inside of the createMany of workexperiences? You're updating Resume. So you should have:
{
where: {
id: "clo0f7ax1000ck93x82umupkv"
},

data: {
personalInfo: {
create/update: {
data: {
name: "ffasdasd",
email: "ff@gmai.com",
phone: "5555555555",
address: "fasfaffaf"
},
}
},
workexperiences: {
createMany: {
data: [
{
position: "fasfafsf",
company: "fasfsa",
startDate: "2023-10-21T18:45:30.123Z",
endDate: "2023-10-21T18:45:30.123Z",
description: "dasdad"
}
],
}
}
}
}
}
Allthought i'm having problems with rhf useFieldArray. When i check formState it says that inputs are dirty(modified) but when i Make a submit i just get an empty {} object from My Form.
Do you Have experience with rhf to know what is causing this kind behavior @Giant panda
Giant panda
you gotta create a new topic