question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After upgrading to 3.0.2, can't create or update records when json field is null

See original GitHub issue

I’m having a similar error to https://github.com/prisma/prisma/issues/9199 after upgrading to Prisma 3.0.2 (from 2.x)

I have also ran prisma migrate with no changes in order to cohere with Upgrade Path for referential actions and named constraints

datamodel in particualar:

model Room {
  id        String    @id @default(cuid()) @db.VarChar(30)
  name      String
  updatedAt DateTime  @updatedAt
  createdAt DateTime  @default(now())
  accountId String    @map("account") @db.VarChar(30)
  bgColor   Json?
  account   Account   @relation(fields: [accountId], references: [id])
  bookings  Booking[]

  @@index([accountId])
}

Mutation from Nexus:

    t.field('createRoom', {
      type: 'Room',
      args: {
        name: nonNull(stringArg()),
        accountId: nonNull(idArg()),
        bgColor: arg({type: "Json"}),
      },
      resolve: (_parent, args, ctx) => ctx.prisma.room.create({
        data: {
          ...args
        }
      })
    });

Error when bgColor is null (the same thing when creating or updating):

Invalid `prisma.room.create()` invocation: { data: { name: 'Test Room', accountId: 'ckm7v6zpi00130huil8hbwvlk', bgColor: null ~~~~ } } Argument bgColor for data.bgColor must not be null. Please use undefined instead.```

_Originally posted by @chemicalkosek in https://github.com/prisma/prisma/issues/9199#issuecomment-919430264_

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
chemicalkosekcommented, Sep 21, 2021

That would mean rewriting even the simplest resolvers such as

 resolve: (_parent, args, ctx) => ctx.prisma.room.create({
        data: {
          ...args
        }
      })

to check for json null values. Can we make a default that will transform the null value to the previous behaviour? Or such thing will not be possible?

6reactions
matthewmuellercommented, Sep 20, 2021

Hey everyone, sorry for the confusion here. This is expected, but we missed this in our documentation. We stopped allowing nulls for JSON field inputs because:

prisma.foo.update({ where: { ... }, data: { someJsonColumn: null }});

The above code is ambiguous:

  • Did you mean setting someJsonField to the value null?
  • Or did you mean setting someJsonField to the database NULL?

The solution is to use DbNull and JsonNull. To insert a null value into a Json field, you would write:

import { Prisma } from '@prisma/client'

prisma.log.create({
  data: {
    meta: Prisma.JsonNull,
  },
})

And to insert a database NULL into a Json field, you would write:

import { Prisma } from '@prisma/client'

prisma.log.create({
  data: {
    meta: Prisma.DbNull,
  },
})

We’re going to improve this error message and also fix up our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I update a JSON column with a NULL value?
Hi Im trying to add / update data in ...
Read more >
JSON reader parses values as null - Azure Databricks
You are attempting to read a JSON file. You know the file has data in it, but the Apache Spark JSON reader is...
Read more >
Migration Guide: SQL, Datasets and DataFrame - Spark 3.0.2 ...
In Spark 3.0, the returned row can contain non- null fields if some of JSON column values were parsed and converted to desired...
Read more >
12 Updating a JSON Document with JSON Merge Patch
If a patch field value of null did not have a special meaning (remove the corresponding source member with that field) then you...
Read more >
Upgrade to Prisma 3
You cannot filter a Json field by a null value. See this GitHub issue. This is because { equals: null } checks if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found