prisma: `Error: Unknown value type` on nested create

Bug description

After upgrading to prisma v5, we are now seeing the following error when executing a nested create query. We have downgraded to 4.16.2 as a workaround. DEBUG=prisma* did not yield any more relevant logs for the error. Will attempt to make a small repro.

error - Error: Unknown value type
    at pe (/.../node_modules/@prisma/client/runtime/library.js:26:706)
    at na (/.../node_modules/@prisma/client/runtime/library.js:115:8408)
    at ia (/.../node_modules/@prisma/client/runtime/library.js:115:8562)
    at na (/.../node_modules/@prisma/client/runtime/library.js:115:8400)
    at ia (/.../node_modules/@prisma/client/runtime/library.js:115:8562)
    at na (/.../node_modules/@prisma/client/runtime/library.js:115:8400)
    at ia (/.../node_modules/@prisma/client/runtime/library.js:115:8562)
    at na (/.../node_modules/@prisma/client/runtime/library.js:115:8400)
    at ia (/.../node_modules/@prisma/client/runtime/libra15:8562)
    at na (/.../node_modules/@prisma/client/runtime/library.js:115:8400) {
  clientVersion: '5.0.0',

How to reproduce

  1. Run nested create query

Expected behavior

Nested create query succeeds.

Prisma information

datasource db {
  provider = "postgres"
  url      = env("TEST_POSTGRES_URI")
}

generator client {
  provider = "prisma-client-js"
  output   = "../node_modules/.prisma/client"
}

model User {
  uuid           String           @id @default(uuid())
  teamUuid       String           @default(uuid())
  userName       String
  UserTemplateV2 UserTemplateV2[]
}

model UserTemplateV2 {
  uuid   String @id @default(uuid())
  userId String
  from   String
  jsonField Json
  user   User   @relation(fields: [userId], references: [uuid])
}
await prisma.user.create({
  select: { uuid: true, teamUuid: true },
  data: {
    userName: name.trim(),
    UserTemplateV2: {
      create: [
        {
          from: `${user.firstName} ${user.lastName}`,
          jsonField: { href: new URL("http://www.example.com") }
        },
        {
          from: `${user.firstName} ${user.lastName}`,
          jsonField: { href: new URL("http://www.example.com") }
        },
      ],
    },
  },
});

Environment & setup

  • OS: Alpine Linux
  • Database: PostgreSQL 14
  • Node.js version: v16.19.0

Prisma Version

5.0.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 23 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @Blitz2145 and @masterbater. I know you mentioned that you’ve used workarounds to update your projects, but in case you’d like to revert that in future update, i’d like to let you know that it’s fixed and will be released as a part of Prisma 5.1. If you’d like to test the fix earlier, you can use dev snapshot version 5.1.0-dev.18. We don’t recommend using in production, but it should be good enough to verify that fix works for you. Thank you!

We’re looking into this, like @Blitz2145, in the previous versions .toJSON() was called by JSON.stringify() and it worked “unexpectedly” in JS without type-checking.

With type-checking it is not possible to have jsonField: { href: new URL("http://www.example.com/") } for example.

There are 2 options at the moment:

  • Downgrade to 4.16.2 and wait for updates here, we are working on a fix and we’ll share a special version that you will be able to try.
  • Change your queries where you have objects, add .toJSON(), like jsonField: { href: new URL("http://www.example.com/").toJSON() }

Note that nothing needs to be changed for Date, Decimal, BigInt or Buffer.

Yea, we just changed our queries to call .toJSON() on the values and we were able to roll-forward to v5.

@wtachau We did not mention in the migration guide because it’s an unexpected bug, which is now fixed and will be included in the next release (5.1.0 scheduled for August 1st).

Oh interesting, I put

{
 "prisma": "^5.1.0-dev.18",
 "@prisma/client": "^5.1.0-dev.18",
}

in my package.json, re-ran yarn and prisma generate and that’s what got generated. I’ll specify 5.1.0-dev.18 exactly later and see if it still happens.

@masterbater Can you please give us some more information on what exactly the schema and the Prisma Client query looked like and which value you had to cast? This sounds different than the URL() example from @Blitz2145 - and we would really like to understand this. Thanks!

This cause error in findUnique, findMany or findFirst when I was passing an ObjectId in id field

const deviceId = ObjectId("507f1f77bcf86cd799439011"); //Simulates deviceId in change streams
  const device = await this.prisma.Devices.findFirst({
          where: { id: deviceId.toString() }, // This fix the issue for me and finally upgraded to prisma 5 with confidence
          select: { device: true },
        });

Thats why that particular query only causes error, got an idea @Jolg42 and remember ObjectId in mongodb are not string You wont have any issue with this if you only use prisma all throughout your application

@wtachau client version in your error message is 5.1.0-integration-fix-client-types-mixed-input-types.1, not 5.1.0-dev.18. Please, make sure to update both @prisma/client and prisma packages and re-run prisma generate after doing so.

Hi @Blitz2145 and @masterbater. I know you mentioned that you’ve used workarounds to update your projects, but in case you’d like to revert that in future update, i’d like to let you know that it’s fixed and will be released as a part of Prisma 5.1. If you’d like to test the fix earlier, you can use dev snapshot version 5.1.0-dev.18. We don’t recommend using in production, but it should be good enough to verify that fix works for you. Thank you!

Thanks for the quick fix I was looking on it on your milestone for 5.1 thanks for including it. Props to you and people working on this and all prisma team for your efforts.

We’re looking into this, like @Blitz2145, in the previous versions .toJSON() was called by JSON.stringify() and it worked “unexpectedly” in JS without type-checking.

With type-checking it is not possible to have jsonField: { href: new URL("http://www.example.com/") } for example.

There are 2 options at the moment:

  • Downgrade to 4.16.2 and wait for updates here, we are working on a fix and we’ll share a special version that you will be able to try.
  • Change your queries where you have objects, add .toJSON(), like jsonField: { href: new URL("http://www.example.com/").toJSON() }

Note that nothing needs to be changed for Date, Decimal, BigInt or Buffer.

@SevInf I’ve updated the description with a schema and query that I think should reproduce the issue. We are passing a URL (https://developer.mozilla.org/en-US/docs/Web/API/URL#instance_methods) value into the json field. Its seems like previously that value was being processed correctly since it implements toJSON but maybe that is no longer being called in v5.