3

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:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-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?
}

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

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:

Raj Chaudhary
  • 1,444
  • 3
  • 16
  • 31
  • So how does TS type looks like then? Did you run `prisma generate`? – Danila Feb 04 '22 at 11:31
  • I did run prisma generate. TS type marks it as a non optional – Raj Chaudhary Feb 07 '22 at 11:24
  • Optional and nullable are different things though. Can you just show the type we are talking about? I've tried your schema locally and it works fine, type is nullable as expected. – Danila Feb 07 '22 at 11:40
  • In which file can I find the code for the generated User type? It's marked as non-nullable for sure though based on VS code intellisense popups. – Raj Chaudhary Feb 08 '22 at 02:36
  • Press F12 one something where you can see the popup. Basically it should be in the `node_modules/.prisma/client/index.d.ts` – Danila Feb 08 '22 at 09:05

1 Answers1

6

When working with JSONB fields, there are two types of null allowed: JSON null and DB null.

This is explained in more detail in the Prisma docs for working with JSON fields.

To avoid this ambiguity between the two types of null allowed by the database, we allow one of the following two values:

  • Prisma.DbNull: The value in the database is a NULL.
  • Prisma.JsonNull: value in the database contains a JSON value that is null.

This is what those two null values look like in the database (same schema as the one you shared):

prisma-script> select id, "oauthData" from "User"
+---------+-------------+
| id      | oauthData   |
|---------+-------------|
| 124124  | null        |
| 1241241 | <null>      |
+---------+-------------+

You can also see this in the generated type for the model User

the generated type for User

Daniel
  • 1,236
  • 1
  • 9
  • 13