The Model. upsert() method is a new method added in Sequelize v6 that allows you to perform an update statement only when a row with matching values already exist. To update a row, you need to specify the primary key of the row, which is the id column in case of the Users table.
While Sequelize doesn't provide a bulkUpdate() method, both update() and bulkCreate() methods allow you to update multiple rows with a single method. When you need to update multiple rows with different values, you can use the bulkCreate() method.
Since version 2.0.0 you need to wrap your where clause in a where
property:
Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
)
.success(result =>
handleResult(result)
)
.error(err =>
handleError(err)
)
The latest version actually doesn't use success
and error
anymore but instead uses then
-able promises.
So the upper code will look as follows:
Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
)
.then(result =>
handleResult(result)
)
.catch(err =>
handleError(err)
)
try {
const result = await Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
)
handleResult(result)
} catch (err) {
handleError(err)
}
http://docs.sequelizejs.com/en/latest/api/model/#updatevalues-options-promisearrayaffectedcount-affectedrows
I have not used Sequelize, but after reading its documentation, it's obvious that you are instantiating a new object, that's why Sequelize inserts a new record into the db.
First you need to search for that record, fetch it and only after that change its properties and update it, for example:
Project.find({ where: { title: 'aProject' } })
.on('success', function (project) {
// Check if record exists in db
if (project) {
project.update({
title: 'a very different title now'
})
.success(function () {})
}
})
Since sequelize v1.7.0 you can now call an update() method on the model. Much cleaner
For Example:
Project.update(
// Set Attribute values
{ title:'a very different title now' },
// Where clause / criteria
{ _id : 1 }
).success(function() {
console.log("Project with id =1 updated successfully!");
}).error(function(err) {
console.log("Project update failed !");
//handle error here
});
January 2020 Answer
The thing to understand is that there's an update method for the Model and a separate update method for an Instance (record). Model.update()
updates ALL matching records and returns an array see Sequelize documentation. Instance.update()
updates the record and returns an instance object.
So to update a single record per the question, the code would look something like this:
SequlizeModel.findOne({where: {id: 'some-id'}})
.then(record => {
if (!record) {
throw new Error('No record found')
}
console.log(`retrieved record ${JSON.stringify(record,null,2)}`)
let values = {
registered : true,
email: '[email protected]',
name: 'Joe Blogs'
}
record.update(values).then( updatedRecord => {
console.log(`updated record ${JSON.stringify(updatedRecord,null,2)}`)
// login into your DB and confirm update
})
})
.catch((error) => {
// do seomthing with the error
throw new Error(error)
})
So, use Model.findOne()
or Model.findByPkId()
to get a handle a single Instance (record) and then use the Instance.update()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With