Arun Kumar
06/30/2021, 7:08 AMcreate
and update
Ryan
06/30/2021, 7:10 AMArun Kumar
06/30/2021, 7:14 AMdata
property in the create
?Arun Kumar
06/30/2021, 7:14 AM'data
property in create
const updatedOrCreatedUser = await prisma.upsertUser({
where: {
email: '<mailto:alice@prisma.io|alice@prisma.io>',
},
update: {
role: 'ADMIN',
},
create: {
name: 'Alice',
email: '<mailto:alice@prisma.io|alice@prisma.io>',
role: 'ADMIN',
},
})
Ryan
06/30/2021, 7:15 AMdata
is not requiredArun Kumar
06/30/2021, 7:52 AMwhere
I have a condition field and in update
should i have that field as well?Ryan
06/30/2021, 7:55 AMArun Kumar
06/30/2021, 7:56 AMidReferral
in where, update and create
Unknown arg `idReferral` in where.idReferral for type ResponsiblePersonWhereUniqueInput.
Argument idReferral for create.idReferral is missing.
Ryan
06/30/2021, 8:03 AMidReferral
is unique and can be used in where
?Arun Kumar
06/30/2021, 8:03 AMArun Kumar
06/30/2021, 8:03 AMRyan
06/30/2021, 8:04 AMupsert
requires a unique field from the same model. A reference is not allowed.Arun Kumar
06/30/2021, 8:06 AMRyan
06/30/2021, 8:07 AMArun Kumar
06/30/2021, 8:10 AMmodel ResponsiblePerson {
id Int @id @default(autoincrement())
firstName String @db.VarChar(150)
street String @db.VarChar(150)
city String @db.VarChar(150)
state String @db.VarChar(150)
zip String @db.VarChar(150)
phone String @db.VarChar(150)
email String @db.VarChar(150)
relationshipToResident String @db.VarChar(150)
lastName String @db.VarChar(150)
country String @db.VarChar(150)
responsibleFor String @db.VarChar(60)
idReferral Int
}
Ryan
06/30/2021, 8:12 AMidReferral
is neither unique nor an id so cannot be used in the where
condition.Arun Kumar
06/30/2021, 8:13 AMArun Kumar
06/30/2021, 8:44 AMRyan
06/30/2021, 9:10 AM0
if you’re using Int id’s if you know that there’s no record.Arun Kumar
06/30/2021, 4:19 PMcreate
method. I didn't provide the id to the create
and it created a new entry.Ryan
06/30/2021, 4:51 PMid
will be generated automatically.Arun Kumar
06/30/2021, 5:52 PMRyan
07/01/2021, 5:20 AMid
is set as default (like autoincrement
) then it will create a new entry with the autogenerated id.Arun Kumar
07/01/2021, 5:33 AMRyan
07/01/2021, 5:36 AMcreate
. Could you elaborate on that?Arun Kumar
07/01/2021, 5:38 AMupdate
if the id isn't present then it should create
the entry.Arun Kumar
07/01/2021, 5:38 AMRyan
07/01/2021, 5:40 AMid
in create
and update
. You only need to pass it in where
.Arun Kumar
07/01/2021, 6:18 AMPrismaClientKnownRequestError2 [PrismaClientKnownRequestError]:
Invalid `prisma.preAdmission1.upsert()` invocation:
Unique constraint failed on the fields: (`idReferral`)
at cb (C:\Users\ArunKadari\Documents\projects\cradleAPI\node_modules\@prisma\client\runtime\index.js:35101:17)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Promise.all (index 1)
at async exports.postOne (C:\Users\ArunKadari\Documents\projects\cradleAPI\controllers\preAdmission1AssesmentController.js:90:21) {
code: 'P2002',
clientVersion: '2.23.0',
meta: { target: [ 'idReferral' ] }
Ryan
07/01/2021, 6:22 AMidReferral
is unique? If so, have you assigned the same value in create
?Arun Kumar
07/01/2021, 6:22 AMArun Kumar
07/01/2021, 6:23 AMArun Kumar
07/01/2021, 6:25 AMvar data = req.body
delete data.id
prisma.preAdmission1.upsert({
where:{
id: req.body.id ? req.body.id : 0
},
update: data,
create: data
});
Ryan
07/01/2021, 6:25 AMidReferral
.Arun Kumar
07/01/2021, 6:27 AMid
exists update
. if no id create
Ryan
07/01/2021, 6:31 AMidReferral
in create, then it will give a unique constraint error.Arun Kumar
07/01/2021, 6:33 AMArun Kumar
07/01/2021, 6:34 AMArun Kumar
07/01/2021, 6:34 AMRyan
07/01/2021, 6:41 AMdata
are you passing in update
and create
?Arun Kumar
07/01/2021, 6:54 AMArun Kumar
07/01/2021, 6:56 AMRyan
07/01/2021, 6:59 AMidReferral
that you’re passing in create
. The unique constraint error is causing that.Ryan
07/01/2021, 7:02 AMArun Kumar
07/01/2021, 7:04 AMRyan
07/01/2021, 7:07 AMArun Kumar
07/01/2021, 7:07 AMArun Kumar
07/01/2021, 7:16 AMupsert({
where:{
id: req.body.id ? req.body.id : 0
},
update: req.data,
create: req.data
})
Ryan
07/01/2021, 7:19 AMArun Kumar
07/01/2021, 7:25 AMArun Kumar
07/01/2021, 7:26 AMRyan
07/01/2021, 7:29 AMid
exist in the database that you’re passing in where
?Arun Kumar
07/01/2021, 7:29 AMRyan
07/01/2021, 7:34 AMArun Kumar
07/01/2021, 7:34 AMRyan
07/01/2021, 7:41 AMArun Kumar
07/01/2021, 7:43 AMArun Kumar
07/01/2021, 7:44 AMRyan
07/01/2021, 7:44 AMid
passed in where
is present in the table, then it will always go to update
. id
is not required to be passed to update
.Ryan
07/01/2021, 7:46 AMPrismaClient
instance
const prisma = new PrismaClient({
log: ['query'],
})
2. First create the data and check if that’s created successfully
3. Then using upsert
pass the same id
created above to where
4. Send me the logs of the queries that were run in steps 2 and 3Arun Kumar
07/01/2021, 7:47 AMArun Kumar
07/01/2021, 7:47 AMArun Kumar
07/01/2021, 7:59 AM`Create`
prisma:query INSERT INTO "public"."PreAdmission1" ("dietryRestrictions","dietryRestrictionDescription","recentChangeInF ake","recentChangeInFooodIntake","recentChangeInFoodIntakeDescription","eatsLessThan3MealsAday","mealsDescription","mouthOrTeethProblem","mouoodIntsToEatWithOthers","recthOrTeethProblemDescription","likesToEatWithOthers","recentlyLostOrGainedWeight","lossOrGain","numberOfPounds","consumeethProisionImpaired","visionsAlcohol","alcoholFrequency","usesDrugs","drugsFrequency","isVisionImpaired","visionImpairmentDetails","isHearingImpairlcohol"isCongnitiveImpaired"ed","hearingImpairmentDetails","isSpeechImpaired","speechImpairmentDetails","hasDentures","isCongnitiveImpaired","congnmentDergery","accidents","isitiveImpairmentDetails","canParticipateInSocialActivities","listOfSocialActivities","healthHistory","majorIllness","sur"canPa,$2,$3,$4,$5,$6,$7,$8,gery","accidents","isHospitalizedInLast5Years","durationOfHospitalization","descriptionOfHospitalization","idReferral",zedInLblic"."PreAdmission1"."overallHealth") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$5,$6,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36) RETURNING "public"."PreAdmission1"."id"
`update`
prisma:query INSERT INTO "public"."PreAdmission1" ("dietryRestrictions","dietryRestrictionDescription","recentChangeInFoodIntake","recentChangeInFoodIntakeDescription","eatsLessThan3MealsAday","mealsDescription","mouthOrTeethProblem","mouthOrTeethProblemDescription","likesToEatWithOthers","recentlyLostOrGainedWeight","lossOrGain","numberOfPounds","consumesAlcohol","alcoholFrequency","usesDrugs","drugsFrequency","isVisionImpaired","visionImpairmentDetails","isHearingImpaired","hearingImpairmentDetails","isSpeechImpaired","speechImpairmentDetails","hasDentures","isCongnitiveImpaired","congnitiveImpairmentDetails","canParticipateInSocialActivities","listOfSocialActivities","healthHistory","majorIllness","surgery","accidents","isHospitalizedInLast5Years","durationOfHospitalization","descriptionOfHospitalization","idReferral","overallHealth") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36) RETURNING "public"."PreAdmission1"."id"
Ryan
07/01/2021, 10:03 AMArun Kumar
07/01/2021, 10:04 AM"@prisma/client": "^2.23.0",
Ryan
07/01/2021, 10:26 AMprisma
and @prisma/client
to the latest and check?
Given this schema:
model User {
id Int @id @default(autoincrement())
name String
}
This query produces the following output:
await prisma.user.upsert({
where: { id: 1 },
create: { name: 'user 1' },
update: { name: 'user 1' },
})
It seems that different query is running rather than upsert