Skip to content

Error when including optional relation when a polymorphic relation is present #1483

@kwerdna19

Description

@kwerdna19

Description and expected behavior

Hello, running into an error when trying to include a model in a findMany that is optional. I will layout the simplest setup I can that reproduced the error for me:

schema.zmodel:

generator client {
    provider = "prisma-client-js"
}

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

model User {
    @@auth
    id    String @id
    edits Edit[]
    @@allow('all', true)
}

model Entity {

    id    String @id @default(cuid())
    name  String
    edits Edit[]

    type  String
    @@delegate(type)

    @@allow('all', true)
}

model Person extends Entity {
}

model Edit {
    id       String  @id @default(cuid())

    authorId String?
    author   User?   @relation(fields: [authorId], references: [id], onDelete: Cascade, onUpdate: NoAction)

    entityId String
    entity   Entity  @relation(fields: [entityId], references: [id], onDelete: Cascade, onUpdate: NoAction)

    @@allow('all', true)
}

As you can see the authorId (and relation User) is optional. When attempting to make a query that includes this model I run into the following error:

TypeError: Object.entries requires that input parameter not be null or undefined

Here is some test code with the above the will throw the error:

import { enhance } from "@zenstackhq/runtime";
import { db } from "~/server/db";

const zen = enhance(db);

await zen.edit.deleteMany({});
await zen.person.deleteMany({});
await zen.user.deleteMany({});

const person = await zen.person.create({
	data: {
		name: "test",
	},
});

await zen.edit.create({
	data: {
		entityId: person.id,
	},
});

// THROWS ERROR HERE
const all = await zen.edit.findMany({
	include: {
		author: true,
	},
});

Oddly enough this does not happen if the model does not have a relation on it that is polymorphic, so if I remove the Entity relation from Edit or replace it with a regular (non-polymorphic) model, then the error doesnt happen!

It will also not happen if the author is specified when the row is created but that might be less surprising.

Thanks!

Environment (please complete the following information):

  • ZenStack version: 2.1.2
  • Prisma version: 5.13
  • Database type: postgres

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions