Skip to content

Create operation does not accept undefined value #765

@iksemot

Description

@iksemot

Description and expected behavior

Method create errors because of undefined as the value of relation.

The workaround is to use {} but undefined is valid and allowed in pure PrismaClient.

Environment (please complete the following information):

  • ZenStack version: 1.0.2
  • Prisma version: 5.4.2
  • Database type: sqlite, postgres

Additional context

Given data model:

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

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  name  String

  post   Post? @relation(fields: [postId], references: [id])
  postId Int?

  @@allow('all', true)
}

model Post {
  id    Int    @id @default(autoincrement())
  title String
  User  User[]

  @@allow('all', true)
}

Following code:

const prisma = enhance(new PrismaClient(), {})
    
const user = await prisma.user.create({
  data: {
    name: 'Me',
    post: undefined
  }
})

Errors with:

Error calling enhanced Prisma method `create`: Cannot convert undefined or null to object
    at C:\Users\tomas\workspace\playground\zenstack_\src\zenstack.ts:9:32,
    at Generator.next (<anonymous>),
    at C:\Users\tomas\workspace\playground\zenstack_\src\zenstack.ts:8:71,
    at new Promise (<anonymous>),
    at __awaiter (C:\Users\tomas\workspace\playground\zenstack_\src\zenstack.ts:4:12),
    at main (C:\Users\tomas\workspace\playground\zenstack_\src\zenstack.ts:16:12),
    at Object.<anonymous> (C:\Users\tomas\workspace\playground\zenstack_\src\zenstack.ts:36:3),
    at Module._compile (node:internal/modules/cjs/loader:1256:14),
    at Module.m._compile (C:\Users\tomas\workspace\playground\zenstack_\node_modules\ts-node\src\index.ts:1618:23) {
  internalStack: 'TypeError: Cannot convert undefined or null to object\n' +
    '    at Function.entries (<anonymous>)\n' +
    '    at NestedWriteVisitor.<anonymous> (C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\src\\enhancements\\nested-write-visitor.ts:327:59)\n' +
    '    at Generator.next (<anonymous>)\n' +
    '    at C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\runtime\\enhancements\\nested-write-visitor.js:10:71\n' +
    '    at new Promise (<anonymous>)\n' +
    '    at __awaiter (C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\runtime\\enhancements\\nested-write-visitor.js:6:12)\n' +
    '    at NestedWriteVisitor.visitSubPayload (C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\runtime\\enhancements\\nested-write-visitor.js:208:16)\n' +
    '    at NestedWriteVisitor.<anonymous> (C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\src\\enhancements\\nested-write-visitor.ts:154:36)\n' +
    '    at Generator.next (<anonymous>)\n' +
    '    at fulfilled (C:\\Users\\tomas\\workspace\\playground\\zenstack_\\node_modules\\@zenstackhq\\runtime\\enhancements\\nested-write-visitor.js:7:58)'
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions