Cannot update a column value to null?

See original GitHub issue

I have a foreign key column in my table, call it parentId, when parentId is null, i know that the current row has no parent. If it is not null then I can use that foreign key to connect with the corresponding parent. When trying to use update in prisma, i run into a few issues:

First, I cannot directly update the value of parentId or any foreign key for that matter, and instead an error is thrown and I am basically told that parentId field doesnt exist and instead what im looking for is the relation field parent. So the below does not work

db.folder.update({
    data: {
      parentId: obj.id
    },
    where: { id },
  })

So when i try to instead update the relation field parent like this:

db.folder.update({
    data: {
      parent:
         {
              connect: {
                id: obj.id,
              },
            },
    },
    where: { id },
  })

Then if obj.id is null, an error is thrown because you cant connect with a null id, so instead i add a conditional,

db.folder.update({
    data: {
      parent:
        obj.id == null
          ? null
          : {
              connect: {
                id: obj.id,
              },
            },
    },
    where: { id },
  })

And this does not work either, instead every time i pass in null, the value in the db is not changed, which i guess is a safeguard so that if no parameter is passed in, it doesnt nullify the value. However, I need to update the value of parentId to be null in certain cases… How can i do that??

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
VladislavShapovalcommented, Jul 15, 2022

if anybody is still looking for answer:

prisma.yourTable.update({
   data: {
      yourFieldName: Prisma.NullableJsonNullValueInput.DbNull,
   },
   where: {
     id: 1,
   }
});
2reactions
pantharshit00commented, May 4, 2020

Thanks @dpetrick, I missed that detail.

@ShehabSN You can use the following client attain the required result:

prisma.folder.update({
     data: {
       parent: {
         disconnect: true
       }
     },
     where: {
       id: 2
     }
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

ORA-01407: cannot update (string) to NULL tips
ORA-01407 occurs as you are trying to change a column to NULL when the column does not accept NULL values. To resolve ORA-01407,...
Read more >
ORA 01407 - cannot update column to null — oracle-tech
Whenever using UPDATE statement always make sure to use EXISTS condition in the WHERE clause to filter only the row that match the...
Read more >
Not able to update column which was set as null using 'select ...
1 Answer 1 · if column is assigned a 'value' of NULL (as in the example: col1 = null ) then ASE will...
Read more >
Oracle / PLSQL: ORA-01407 Error Message - TechOnTheNet
You tried to update a column to a NULL value but the column will not accept NULL values. Resolution. The option(s) to resolve...
Read more >
Updating a Column to NULL - IBM
Use the NULL keyword to modify a column value when you use the UPDATE statement. For example, for a customer whose previous address...
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