Prisma client not marking property as null
See original GitHub issueBug description
I am using Prisma version 3.8.1. Prisma client does not mark the User.oauthData property as nullable in TS. Can someone help? Prisma schema and generated SQL files below:
How to reproduce
Run prisma generate with attached schema file on MacOS (Postgres)
Expected behavior
No response
Prisma information
Schema:
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Account {
id BigInt @id
createdAt DateTime @db.Timestamptz(3) @default(now())
updatedAt DateTime @db.Timestamptz(3) @updatedAt
name String
users User[]
}
model User {
id BigInt @id
createdAt DateTime @db.Timestamptz(3) @default(now())
updatedAt DateTime @db.Timestamptz(3) @updatedAt
accountId BigInt
account Account @relation(fields: [accountId], references: [id])
fullName String
email String
oauthData Json?
}
SQL:
-- CreateTable
CREATE TABLE "Account" (
"id" BIGINT NOT NULL,
"createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ(3) NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "User" (
"id" BIGINT NOT NULL,
"createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ(3) NOT NULL,
"accountId" BIGINT NOT NULL,
"fullName" TEXT NOT NULL,
"email" TEXT NOT NULL,
"oauthData" JSONB,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Environment & setup
- OS: Mac OS
- Database: PostgreSQL
- Node.js version: 16.13.1
Prisma Version
prisma : 3.8.1
@prisma/client : 3.8.1
Current platform : darwin
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio : 0.452.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Prisma client not marking property as null
I am using Prisma version 3.8.1. Prisma client does not mark the User.oauthData property as nullable in TS. Can someone help? Prisma schema...
Read more >Null and undefined (Reference)
Prisma Client differentiates between null and undefined : null is a value; undefined means do nothing. Note: This is particularly important to account...
Read more >Prisma Client API (Reference)
Fields that are marked as optional or have default values in the datamodel are optional. select, XOR<UserSelect, null>, No, Specifies which properties to ......
Read more >Transactions
Explore techniques for handling transactions with Prisma Client. ... The $transaction API does not allow you to pass IDs between distinct operations.
Read more >Working with Json fields (Concepts)
You are importing data from another system and do not want to map that data to Prisma models. Reading a Json field. You...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just an FYI in your test there, you are explicitly removing the JSON field from your
create
statement, leaving the default value to be handled by the database, not prisma. The problem occures when you’re trying to specifically, purposely, set a JSON value as null.Prismas validators let the JSON field pass as optional when not defined, (
undefined
), but it’s wrongly marking it as invalid when you explictly set the JSON field tonull
😕 In other words, like in my case, I can create a row with null fields by not including the field / setting them as undefined, but once I set some data in the JSON field, I can never change it back to null, I have temporarily solved it by using empty arrays.Just to clarify: this is about the error message the engine throws at runtime. The above code does produce a type error when compiling in Typescript.
One easy way to reproduce is,
And just run the above javascript file directly.