Prisma nested write issue

I get a TS error (and failing query) when trying to execute this nested write.
model Applicant {
id String @id @default(uuid()) @db.Uuid
email String @unique
applications Application[]

@@map("applicants")
}

model Application {
id String @id @default(uuid()) @db.Uuid
name String
applicant Applicant? @relation(fields: [applicantId], references: [id])
applicantId String? @map("applicant_id") @db.Uuid

@@index([applicantId])
@@map("applications")
}
model Applicant {
id String @id @default(uuid()) @db.Uuid
email String @unique
applications Application[]

@@map("applicants")
}

model Application {
id String @id @default(uuid()) @db.Uuid
name String
applicant Applicant? @relation(fields: [applicantId], references: [id])
applicantId String? @map("applicant_id") @db.Uuid

@@index([applicantId])
@@map("applications")
}
prisma.application.create({
data: { // Types of property 'name' are incompatible. Type 'string' is not assignable to type 'undefined'
name: "whatever",
applicant: {
create: {
},
},
},
})
prisma.application.create({
data: { // Types of property 'name' are incompatible. Type 'string' is not assignable to type 'undefined'
name: "whatever",
applicant: {
create: {
},
},
},
})
What is going on here? Why is this not a valid nested write? When executing the error throws: Unknown arg "applicant" in data.applicant for type ApplicationUncheckedCreateInput.
6 Replies
jdsl
jdsl2y ago
That looks correct when I test it. Did you run prisma generate after updating schema? Restart TS Server and restart VS Code if that doesn't work.
riccardolardi
riccardolardiOP2y ago
@joerambo the error still remains. I'm completely dazzled why this doesn't work I figured it out with the help of Prisma support:
When performing a create operation, data is the type of XOR<ApplicationCreateInput,ApplicationUncheckedCreateInput > which means you can either provide ApplicationCreateInput which will have all nested relations specified or you can directly provide the ID as in the case of applicantId field but you can’t mix them. So you will need to use the fields specified in ApplicationUncheckedCreateInput in order to create it. Also, Unchecked input types allow you to perform some operations that Prisma considers dangerous like directly writing foreign keys. Prisma allows you to choose either a safe or an unchecked input type when doing operations like create. In this case, Prisma wants you to perform the create action using the Unchecked input type.
When performing a create operation, data is the type of XOR<ApplicationCreateInput,ApplicationUncheckedCreateInput > which means you can either provide ApplicationCreateInput which will have all nested relations specified or you can directly provide the ID as in the case of applicantId field but you can’t mix them. So you will need to use the fields specified in ApplicationUncheckedCreateInput in order to create it. Also, Unchecked input types allow you to perform some operations that Prisma considers dangerous like directly writing foreign keys. Prisma allows you to choose either a safe or an unchecked input type when doing operations like create. In this case, Prisma wants you to perform the create action using the Unchecked input type.
jdsl
jdsl2y ago
Can you share the fixed create method? Curious what it looks like now.
riccardolardi
riccardolardiOP2y ago
@joerambo sure here you go
prisma.application.create({
data: {
form: {
connect: {
id: input.formId,
},
},
title: input.content["application-title"],
content: input.content,
applicant: {
connectOrCreate: {
where: {
email: input.content["applicant-email"],
},
create: {
fullname: input.content["applicant-fullname"],
email: input.content["applicant-email"],
address: input.content["applicant-address"],
phone: input.content["applicant-phone"],
},
},
},
},
});
prisma.application.create({
data: {
form: {
connect: {
id: input.formId,
},
},
title: input.content["application-title"],
content: input.content,
applicant: {
connectOrCreate: {
where: {
email: input.content["applicant-email"],
},
create: {
fullname: input.content["applicant-fullname"],
email: input.content["applicant-email"],
address: input.content["applicant-address"],
phone: input.content["applicant-phone"],
},
},
},
},
});
jdsl
jdsl2y ago
Ahh thanks. I see the multiple nested fields now. Appreciate the follow up!
riccardolardi
riccardolardiOP2y ago
GitHub
Nested write issue · prisma prisma · Discussion #18877
I get a TS error (and failing query) when trying to execute this nested write. model Applicant { id String @id @default(uuid()) @db.Uuid email String @unique applications Application[] @@map("...
Want results from more Discord servers?
Add your server