'Sequelize Instance.destroy(...) does not return any information if rows has been deleted or not
May be related to this open issue: instance.destroy(...) does not return any information if rows has been deleted or not but that seems to be more related to Model.destroy(...) which according to documentation should return the number of rows deleted.
I am new to Sequelize so please correct me if I am doing something wrong. I am using it with MySQL 8.0.16 and running sequelize 5.21.2
When using Model.findAll(...) and then looping through the rows and processing a Instance.destroy() I am being returned the instance per the Instance.destroy(...) documentation
db.tags.findAll({where: MyWhereObj, attributes: MyFieldArray}).then(tags => {
_.forEach(tags, tag => {
//more processing scripts for tag
tag.destroy().then(deleted => {
console.log(deleted)
})
})
})
What I would like to know is if the row was successfully deleted. I browsed through the returned instance looking if such an attribute like isNewRecord existed for isDeletedRecord but no similar
I can watch the node console and see the query come through correctly. Is it safe and best practice to assume the row was deleted successfully?
The query is sent to MySQL as...
DELETE FROM `tags` WHERE `guid` = 'aaa-bbb-ccc-ddd'
...which should the number of rows deleted, but Sequelize seems to ignore that information
I am thinking that I can catch a deletion error or a retrieving/processing error using these catch blocks. But wondering if there is a better way to simply know if the deletion took place or not
db.tags.findAll({where: MyWhereObj, attributes: MyFieldArray}).then(tags => {
_.forEach(tags, tag => {
// more processing scripts for tag
tag.destroy().then(deleted => {
console.log(deleted)
}).catch(err =>{
// The row was not deleted
console.error(err)
})
})
}).catch(err => {
// Error in Retrieving/Processing
console.error(err)
})
Solution 1:[1]
Note that there are two destroy methods:
https://sequelize.org/v6/class/src/model.js~Model.html#static-method-destroy and https://sequelize.org/v6/class/src/model.js~Model.html#instance-method-destroy
The v6 documentation says the static method returns Promise<number>, and the instance method returns a Promise (it does not specify what type the promise resolves to).
Also note that some methods return a different type for different dialects. For example, the model static method decrement documents its return value as:
Promise<Model[], ?number> returns an array of affected rows and affected count with options.returning true, whenever supported by dialect
Using sqlite dialect, I have found the destroy instance method to return a promise to the deleted model, not a promise to the deleted row count.
I think the documentation is incorrect, and (to answer your question), I don't think there is a simple way to confirm success of the deletion. I tried this:
const alloc = {code to get the model}
const alloc2 = alloc;
const deletedRowsCount = await alloc.destroy();
const destroy2Result = await alloc2.destroy();
and the when I logged the results with console.log({ deletedRowsCount, destroy2Result }), I got the exact same output for both destroys:
{
deletedRowsCount: allocs {
dataValues: {
id: 2,
amount: null,
locked: false,
createdAt: 2022-02-26T20:35:46.139Z,
updatedAt: 2022-02-26T20:35:46.139Z,
genLedgerAcctId: 9,
tranId: 1
},
_previousDataValues: {
id: 2,
amount: null,
locked: false,
createdAt: 2022-02-26T20:35:46.139Z,
updatedAt: 2022-02-26T20:35:46.139Z,
genLedgerAcctId: 9,
tranId: 1
},
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
destroy2Result: allocs {
dataValues: {
id: 2,
amount: null,
locked: false,
createdAt: 2022-02-26T20:35:46.139Z,
updatedAt: 2022-02-26T20:35:46.139Z,
genLedgerAcctId: 9,
tranId: 1
},
_previousDataValues: {
id: 2,
amount: null,
locked: false,
createdAt: 2022-02-26T20:35:46.139Z,
updatedAt: 2022-02-26T20:35:46.139Z,
genLedgerAcctId: 9,
tranId: 1
},
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}
}
I plan to update this answer for other database types, in particular when I use a dialect that support foreign keys, so I can have a destroy that DOES fail.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NULL pointer |
