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.

Model.update() set null on values which are not given

See original GitHub issue

For me this is a bug. Let’s say I have a table ‘users’ with 4 columns: ‘firstName’, ‘lastName’, ‘createdAt’, ‘updatedAt’. I do:

User.update({ firstName: 'Jane' }, { where: { id: 1 } })

The SQL query looks like this:

UPDATE "users" SET "first_name"="Jane","last_name"=NULL,"created_at"=NULL,"updated_at"=NULL WHERE "id" = '1'

Why other values are set to NULL ? I think the correct update should look like this:

UPDATE "users" SET "first_name"="Jane" WHERE "id" = '1'

I can’t imagine situation when I would like on update null-ify other values.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
writebeatcommented, May 29, 2018

Workaround is to set the fields option on the update function to Object.keys(req.body), worked for me, at least.

1reaction
jony89commented, Aug 12, 2017

@sw-yx that’s probably because u experiencing what I’ve described here : #7056, I’ll try to create PL again for this this week

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to .update() value to NULL in sequelize - Stack Overflow
My model does allowNull which I believe is what allows null values to be the default as well as set: testCol: { type:...
Read more >
SQL Server: Foreign Keys with set null on delete
It means that the child data is set to NULL when the parent data is deleted or updated. SET DEFAULT. It is used...
Read more >
Update Model from database does not update Nullable Property
i.e. When I change a column in the database from NOT NULL to NULL and then run the Update wizard, I see the...
Read more >
How to Replace Nulls with 0s in SQL - The Data School
Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and...
Read more >
Working with SQL NULL values - SQLShack
In terms of the relational database model, a NULL value indicates an ... The AVG () is used to calculate the average value...
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