即使偏执狂设置为true,Sequelize.js仍会删除表行 [英] Sequelize.js still deletes table row even if paranoid is set to true

查看:98
本文介绍了即使偏执狂设置为true,Sequelize.js仍会删除表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让Sequelize.js软删除表中的行。我使用Sequelize cli进行了所有迁移,并且没有使用同步功能在启动时重新同步数据库。我在迁移和模型中都有时间戳记字段,甚至还有DeletedAt字段(模型也有偏执狂:也为true),无论它仍然删除该行,而不是将时间戳记添加到DeletedAt字段中。我注意到,在进行任何查询时,都不会像在某些教程中看到的那样在查询中添加DeletedAt = NULL。我正在使用Sequelize.js v3.29.0。

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.

模型文件:

'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;
};

迁移文件:

'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);
            });
        }
    });


推荐答案

确保偏执狂是在第二个对象param中定义的属性。

Make sure paranoid is attribute defined inside second object param.

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

您已将偏执定义为3。参数就是问题。

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

这篇关于即使偏执狂设置为true,Sequelize.js仍会删除表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆