set unix timestamp to deleted_at field on soft delete actions
See original GitHub issueI’m trying to set deletedAt field with a custom unix timestamp other than the SequelizeJS default datetime format. I mapped the deletedAt field to my database field name using the model define.
const ProductsCategories = sequelize.define(
'PRODUCTS_CATEGORIES',
{
id: {
primaryKey: true,
type: Sequelize.INTEGER,
allowNull: true,
autoIncrement: true
},
business_id: {
type: Sequelize.INTEGER,
allowNull: false
},
branch_id: {
type: Sequelize.INTEGER,
allowNull: false
},
title: {
type: Sequelize.TEXT,
allowNull: false
},
createdAt: { type: Sequelize.INTEGER, field: 'created_at' },
updatedAt: { type: Sequelize.INTEGER, field: 'updated_at' },
deletedAt: { type: Sequelize.INTEGER, field: 'deleted_at' }
},
{
timestamps: true,
underscored: true,
paranoid: true,
tableName: 'products_categories',
hooks: {
beforeCreate: (rec, opt) => {
rec.dataValues.createdAt = Math.floor(Date.now() / 1000)
rec.dataValues.updatedAt = Math.floor(Date.now() / 1000)
},
beforeUpdate: (rec, opt) => {
console.log("INVOKED : beforeUpdate()")
rec.dataValues.updatedAt = Math.floor(Date.now() / 1000)
},
beforeDestroy: (instance, options) => {
console.log('----------------------------------------------')
console.log("INVOKED : beforeDestroy()")
instance.dataValues.deletedAt = Math.floor(Date.now() / 1000)
console.log(instance.dataValues)
console.log('----------------------------------------------')
}
}
}
)
and now I’m trying to soft delete a single record using my Controller.
ProductsCategories.destroy({
where: {
id: { [Op.eq]: req.body.id }
},
individualHooks: true
})
.then(Response => {
console.log('----------------------------------------------')
console.log('----------------------PASS--------------------')
console.log('----------------------------------------------')
})
.catch(e => {
console.log('----------------------------------------------')
console.log('---------------------ERROR--------------------')
console.log( e )
console.log('----------------------------------------------')
})
But above method is not working because Sequelize is failed to update deleted_at field in the database table & throw the following error.
DatabaseError [SequelizeDatabaseError]: Incorrect integer value: 'Sun Jul 07 2019 16:16:34 GMT+0530 (India Standard Time)' for column `database_name`.`products_categories`.`deleted_at` at row 1
Seems like the beforeDestroy() hook is not working as expected. I also opened a stackoverflow question but seems like it’s a bug. 😢
To Reproduce Steps to reproduce the behavior:
- Define new model with beforeDestroy hook
- Try to update deleted_at field with custom unix timestamp on beforeDestroy hook.
What do you expect to happen?
Sequelize to soft delete my record by adding unix timestamp to deleted_at field.
What is actually happening?
Sequelize produce a error saying that Incorrect integer value
Environment
Dialect:
- mysql
Dialect library version: mysql2 1.6.5 Database version: MariaDB 10.3.15 Sequelize version: 5.9.4 Node Version: v11.15.0 OS: Arch Linux
Tested with latest release:
- Yes, version: Sequelize v5.9.4
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (4 by maintainers)
The same behavior was noticed some time ago with instance
destroy()
: https://github.com/sequelize/sequelize/issues/9318 And it was fixed here: https://github.com/sequelize/sequelize/pull/9319 A follow up to refactor bulk destroy was mentioned in the PR but apparently never started.I’m having the same issue. is anyone know how to fix ?