Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize.js still deletes table row even if paranoid is set to true

I'm having trouble getting Sequelize.js to soft delete the rows in my table. I used Sequelize cli to do all my migrations and I'm not using the sync feature to resync the database on start. I have the timestamp fields and even the deletedAt field in my migration and models (model has paranoid: true also) and no matter what it still deletes the row instead of adding a timestamp to the deletedAt field. I noticed when do any querying it doesn't add the deletedAt = NULL in the query like I've seen in some tutorials. I'm using Sequelize.js v3.29.0.

Model File:

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Collection = sequelize.define('Collection', {
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
          isInt: true
      }
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    description: DataTypes.TEXT,
    createdAt: {
        allowNull: false,
        type: DataTypes.DATE
    },
    updatedAt: {
        allowNull: false,
        type: DataTypes.DATE
    },
    deletedAt: {
        type: DataTypes.DATE
    }
  }, {
    classMethods: {
      associate: function(models) {
        Collection.belongsTo(models.User, { foreignKey: 'userId' })
      }
    }
  }, {
    timestamps: true,
    paranoid: true
  });
  return Collection;
};

Migration File:

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('Collections', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      userId: {
        allowNull: false,
        type: Sequelize.INTEGER
      },
      name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      description: {
        type: Sequelize.TEXT
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      deletedAt: {
          type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('Collections');
  }
};

Here is the code in the controller I'm using to destroy the collection object.

Collection.findOne({
        where: {
            id: collectionId,
            userId: user.id
        }
    }).then(function(collection){
        if (collection !== null) {
            collection.destroy().then(function(){
                res.redirect('/collection');
            }).catch(function(error){
                res.redirect('/collection/'+collectionId);
            });
        }
    });
like image 235
bach942 Avatar asked Oct 15 '25 07:10

bach942


1 Answers

Make sure paranoid is attribute defined inside second object param.

..., {
classMethods: {
    associate: function(models) {
        Collection.belongsTo(models.User,{ foreignKey: 'userId' })
      }
  },
    timestamps: true,
    paranoid: true
}

You've defined paranoid as 3. Param and that is the problem.

like image 113
djra Avatar answered Oct 17 '25 21:10

djra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!