Prisma-client-js: Cannot update with null or undefined

Created on 23 Jul 2019  路  3Comments  路  Source: prisma/prisma-client-js

I am trying to remove a value from an optional property, but I cannot find how to do it.

schema.prisma

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

generator photon {
  provider = "photonjs"
}

model User {
  id   String  @default(cuid()) @id @unique
  name String?
}

index.js

const user = await photon.users.create({
    data: {
      name: 'The User'
    }
  })
  const updatedUser = await photon.users.update({
    where: {
      id: user.id
    },
    data: {
      name: null
    }
  })
  console.log({ user, updatedUser })

Console

Argument name: Got invalid value null on photon.updateOneUser. Provided null, expected String.

Note: If I use undefined instead of null, I won't have an error, but the user and updatedUser will have the same values so it doesn't work either

bu1-repro-available

Most helpful comment

When setting '', I have an empty string on the field, but that is not what I want.

In my opinion, an empty string and null are different in essence. It is the same as the difference between 0 for an Int and null. On one hand, you have a value (whatever it is and how it is interpreted as) and on the other hand, you don't have a value. An empty string isn't always interpreted as "not a value", so shouldn't be equivalent.

Moreover, I find it strange that when you create with an optional field you would have null on the field, but when you want to remove it, you then have ''. That's not consistent

All 3 comments

@Errorname because, as the error says expected String, what happens if you set the value to an empty string '' ?

I understand the practice of using null to designate fields that have no value assigned, but in the pattern of type safety, name being of type string, it seems that null (type object) and undefined (type undefined) would be invalid. Whereas '' (type string) should be allowed.

Thoughts?

When setting '', I have an empty string on the field, but that is not what I want.

In my opinion, an empty string and null are different in essence. It is the same as the difference between 0 for an Int and null. On one hand, you have a value (whatever it is and how it is interpreted as) and on the other hand, you don't have a value. An empty string isn't always interpreted as "not a value", so shouldn't be equivalent.

Moreover, I find it strange that when you create with an optional field you would have null on the field, but when you want to remove it, you then have ''. That's not consistent

I also want to insert null to field in Postgres... have you achieved it in any way?

Because if I try to insert a 0 it just says: Expected a valid parent ID to be present for a nested connect on a one-to-many relation. how do I insert a null?

Well, I found a solution but this is because I have a one-to-many relation 馃憠馃徏 users: { disconnect: true } just had to disconnect or remove the relation. In case is useful for someone.

Was this page helpful?
0 / 5 - 0 ratings