Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Cannot update with null or undefined #148

Closed
Errorname opened this issue Jul 23, 2019 · 3 comments · Fixed by #149
Closed

Cannot update with null or undefined #148

Errorname opened this issue Jul 23, 2019 · 3 comments · Fixed by #149
Assignees
Labels
bug/1-repro-available A reproduction exists and needs to be confirmed.
Milestone

Comments

@Errorname
Copy link
Contributor

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

@gihrig
Copy link
Contributor

gihrig commented Jul 23, 2019

@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?

@Errorname
Copy link
Contributor Author

Errorname commented Jul 23, 2019

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

@schickling schickling added bug/0-needs-info More information is needed for reproduction. release/preview3 bug/1-repro-available A reproduction exists and needs to be confirmed. and removed bug/0-needs-info More information is needed for reproduction. labels Jul 24, 2019
@janpio janpio added this to the Preview 3 milestone Aug 9, 2019
@sk8Guerra
Copy link

sk8Guerra commented Jul 24, 2020

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/1-repro-available A reproduction exists and needs to be confirmed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants