Prisma 客户端未将属性标记为 null

Raj*_*ary 3 prisma prisma2

我使用的是 Prisma 版本 3.8.1。Prisma 客户端不会在 TS 中将 User.oauthData 属性标记为可为空。有人可以帮忙吗?Prisma 架构和生成的 SQL 文件如下:

// 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?
}

Run Code Online (Sandbox Code Playgroud)
-- 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;

Run Code Online (Sandbox Code Playgroud)

我使用的是 Prisma 版本 3.8.1。Prisma 客户端不会在 TS 中将 User.oauthData 属性标记为可为空。有人可以帮忙吗?Prisma 架构和生成的 SQL 文件如下:

Dan*_*iel 6

使用 JSONB 字段时,允许两种类型的 null:JSON null 和 DB null。

有关使用 JSON 字段的 Prisma 文档对此进行了更详细的解释。

null为了避免数据库允许的两种类型之间的歧义,我们允许使用以下两个值之一:

  • Prisma.DbNull: 数据库中的值为 NULL。
  • Prisma.JsonNull:数据库中的值包含为 null 的 JSON 值。

这就是这两个空值在数据库中的样子(与您共享的架构相同):

prisma-script> select id, "oauthData" from "User"
+---------+-------------+
| id      | oauthData   |
|---------+-------------|
| 124124  | null        |
| 1241241 | <null>      |
+---------+-------------+
Run Code Online (Sandbox Code Playgroud)

您还可以在模型的生成类型中看到这一点User

为 User 生成的类型