question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma client not marking property as null

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fjeddycommented, Mar 31, 2022

Am I missing something?

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 to null 😕 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.

1reaction
eviefpcommented, Oct 13, 2022

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,

model Repro {
  id          Int        @id @default(autoincrement())
  json        Json?
}
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient()

async function main() {
  await prisma.repro.create({
    data: {
      j: null
    }
  })
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

And just run the above javascript file directly.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found