How to include the deleted elements when querying a “paranoid” table on sequelize.js?

I want that previously existing users on my application, that have been deleted using the paranoid mode (so now its field deletedAt is NOT null), to be able to register again using the same email. So when the API notices a user creation with a previously used email it sets to null the deletedAt field of the previously existing register instead of creating a new user.

Usually for looking for an user I will do,

User.find( { where: { email: } })

But upon inspection of the SQL query created, it includes

Users.deletedAt IS NULL

Is there any special way of finding when dealing with paranoid models?

Just run your query like this:

  where: {email:}, 
  paranoid: false

Sequelize has this feature built in. Per their API docs, you can include the ‘paranoid’ flag in the options of your find call.


User.find({where: {email:}}, {paranoid: false}).success(models) {
    //models contains both deleted and non-deleted users


You can use hook.

var uuid = require('node-uuid')
module.exports = function (sequelize, DataTypes) {
    return sequelize.define("multi_route", {
        email: {
            type: DataTypes.STRING,
            unique: false,
        //other fields
    }, {
        timestamps: true,
        paranoid: true,
        hooks: {
            beforeUpdate: function (multiFare, next) {                  // berforeUpdate will called after beforeDestroy
                if ("_____destroyed") > -1) {   // check contains '_____destroyed' string
           = + uuid.v1()       // set unique value
            beforeDestroy: [function (multiFare, next) {                // beforeDestroy will called before one instance destroyed
       = + '_____destroyed'    // flag this will destroy

Sorry for my bad comments 🙁

Read More:   How to check empty object in angular 2 template using *ngIf

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts