Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

543
Visualizações
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);
            });
        }
    });
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda