P
Prisma5d ago
Peform

strictUndefinedChecks undesired behavior

hey, I am really happy to see the new preview feature strictUndefinedChecks . However, I did run into some confusion testing. When using it I notice that errors are thrown during create and update operations, specifically when an undefined value is used in the data object of the query. Using the following schema as an example:
model TrpcQueries {
id Int @id @default(autoincrement())
correlationId String
path String
duration Int
userId String?
ipv4 String?
createdAt DateTime @default(now())

User User? @relation(fields: [userId], references: [id])

@@index([userId])
@@index([correlationId])
}
model TrpcQueries {
id Int @id @default(autoincrement())
correlationId String
path String
duration Int
userId String?
ipv4 String?
createdAt DateTime @default(now())

User User? @relation(fields: [userId], references: [id])

@@index([userId])
@@index([correlationId])
}
In this schema, ipv4 is optional. However, when I use a header that might be undefined, like so:
await prisma.trpcQueries.create({
data: {
correlationId: "id...",
path: "path",
duration: 123,
ipv4: ctx.headers?.get('x-forwarded-for'),
},
});
await prisma.trpcQueries.create({
data: {
correlationId: "id...",
path: "path",
duration: 123,
ipv4: ctx.headers?.get('x-forwarded-for'),
},
});
I was wondering why an undefined value isn’t allowed here. If undefined values are indeed disallowed, wouldn’t it be helpful if the types only accepted a string or null instead? This way, any potential issues could be caught during development instead of at runtime.
3 Replies
Prisma AI Help
You selected the carefully hand-crafted route. A dev artisan will respond soon. Meanwhile, the #ask-ai channel awaits if you're curious!
Nurul
Nurul4d ago
Can you try using Prisma.Skip?
await prisma.trpcQueries.create({
data: {
correlationId: "id...",
path: "path",
duration: 123,
ipv4: ctx.headers?.get('x-forwarded-for') ?? Prisma.skip,
},
});
await prisma.trpcQueries.create({
data: {
correlationId: "id...",
path: "path",
duration: 123,
ipv4: ctx.headers?.get('x-forwarded-for') ?? Prisma.skip,
},
});
The Prisma.skip symbol can be used to explicitly omit the field from the query if it is undefined.
Peform
PeformOP4d ago
I think this is a little annoying having to do this every time though no? This does fix the issue, but having to remember to do this everytime we update or create a value that is potentially undefined is a bit cumbersome? this doesn't really address the full problem of the findFirst. Before this new preview feature came into effect we'd have to remember to check if the field we were querying by was undefined or not, we seemlying still need to remember to do this for every potentially undefined field? Just going back to the original post,
If undefined values are indeed disallowed, wouldn’t it be helpful if the types only accepted a string or null instead? This way, any potential issues could be caught during development instead of at runtime.
I think this would be a major improvement, and it wouldn't require us to remember to use Prisma.skip everytime we use an undefined field? I think the current implementation of this will just introduce more bugs because we get no intellisense from typescript saying that this could potentially cause an error due to prisma not allowing undefined values

Did you find this page helpful?