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.

set unix timestamp to deleted_at field on soft delete actions

See original GitHub issue

I’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:

  1. Define new model with beforeDestroy hook
  2. 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:open
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nahdelgadocommented, Feb 6, 2020

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.

1reaction
kusmpthcommented, Jul 25, 2019

I’m having the same issue. is anyone know how to fix ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unix Timestamp for deleted_at field and soft deletes - Laracasts
When I have the column set up as an INT, and when I attempt to delete a row, I get prompted with the...
Read more >
unix timestamps in createdAt, updatedAt & deletedAt fields
One thing to try - the individualHooks option calls the beforeDestroy() hook for each instance. This can be applied at the query level:...
Read more >
Fluent → Model - Vapor Docs
Set when a model is deleted from the database. See soft delete. @Timestamp 's date value is optional and should be set to...
Read more >
Timestamps and Soft Delete with Django - Medium
Timestamps are set. But the article needs approval to be published. When the article is approved, the updated_at timestamp is updated.
Read more >
How it works: DynamoDB Time to Live (TTL)
If the attribute is a Number data type, the attribute's value is a timestamp in Unix epoch time format in seconds, and the...
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