Nested write issue #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("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: {
email: "some@email.com",
},
},
},
}) What is going on here? Why is this not a valid nested write? When executing the error throws: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Hi @riccardolardi 👋 I'm unable to reproduce this as the nested write works correctly for me. Can you please share a repository so i can debug this further? |
Beta Was this translation helpful? Give feedback.
-
Hi @ludralph thanks for looking into this. I'm sorry I can't share the whole repo right but I can share the schema and the tRPC router with you, maybe I am doing something wrong in the schema: generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
relationMode = "prisma"
}
model Organization {
id String @id @default(uuid()) @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)
name String?
address String?
forms Form[]
profiles Profile[]
@@map("organizations")
}
model Profile {
id String @id(map: "users_pkey") @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)
email String? @unique(map: "users_email_key")
fullname String?
phone String?
passwordSet Boolean? @default(false) @map("password_set")
role Role @default(USER)
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String? @db.Uuid
@@index([organizationId])
@@map("profiles")
}
enum Role {
USER
MEMBER
OWNER
ADMIN
}
model Applicant {
id String @id @default(uuid()) @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)
email String @unique
fullname String?
address String?
phone String?
applications Application[]
@@map("applicants")
}
model Application {
id String @id @default(uuid()) @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)
form Form @relation(fields: [formId], references: [id])
formId String @map("form_id") @db.Uuid
applicant Applicant? @relation(fields: [applicantId], references: [id])
applicantId String? @map("applicant_id") @db.Uuid
title String?
content Json?
@@index([formId])
@@index([applicantId])
@@map("applications")
}
model Form {
id String @id @default(uuid()) @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)
name String?
apiKey String @unique @map("api_key")
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String @map("organization_id") @db.Uuid
applications Application[]
@@index([organizationId])
@@map("forms")
} create: publicProcedure
.input(z.object({
formId: z.string().uuid(),
applicantId: z.string().uuid().optional(),
content: z.object({
"application-title": z.string(),
"applicant-fullname": z.string(),
"applicant-email": z.string().email(),
"applicant-address": z.string(),
"applicant-phone": z.string(),
}),
}))
.mutation(async ({ ctx, input }) => {
return await ctx.prisma.application.create({
data: { // Types of property 'name' are incompatible. Type 'string' is not assignable to type 'undefined'
formId: input.formId,
applicantId: input.applicantId || applicant?.id,
title: input.content["application-title"],
content: input.content,
applicant: {
create: {
fullname: input.content["applicant-fullname"],
email: input.content["applicant-email"],
address: input.content["applicant-address"],
phone: input.content["applicant-phone"],
},
},
},
});
}), |
Beta Was this translation helpful? Give feedback.
-
Hi Raphael
The type of ApplicationUncheckedCreateInput is:
export type ApplicationUncheckedCreateInput = {
id?: string
created_at?: Date | string
updated_at?: Date | string | null
formId: string
applicantId?: string | null
title?: string | null
content?: NullableJsonNullValueInput | InputJsonValue
}
I don’t understand why there is no “applicant” field, according to the schema (#18877 (comment)) it should be possible to do a nested write, no?
Thanks, best regards
Riccardo
–
Riccardo Lardi
Software Engineering & Interaction Design
www.riccardolardi.com
***@***.*** / +41 79 339 65 38
LinkedIn / GitHub / Twitter
…On 25 Apr 2023 at 11:40 +0200, Raphael Etim ***@***.***>, wrote:
Hi @riccardolardi, what are the fields allowed for the type ApplicationUncheckedCreateInput in /node_modules/.prisma/client/index.d.ts. That should give you a clue of the allowable fields for the application query.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks a lot Raphael, that was it. It works now.
Great support!
All best,
Riccardo
–
Riccardo Lardi
Software Engineering & Interaction Design
www.riccardolardi.com
***@***.*** / +41 79 339 65 38
LinkedIn / GitHub / Twitter
…On 25 Apr 2023 at 21:08 +0200, Raphael Etim ***@***.***>, wrote:
Hi @riccardolardi
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.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I came across the same problem a few times, so in order to avoid the confusion I usually create a separate
|
Beta Was this translation helpful? Give feedback.
Hi @riccardolardi
When performing a
create
operation, data is the type ofXOR<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 ofapplicantId
field but you can’t mix them. So you will need to use the fields specified inApplicationUncheckedCreateInput
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 w…