Error ondelete Scade sequelize

Asked

Viewed 14 times

-1

I am in error when I will do the cascade deletion , using postgres sequelize in version 6.

original: error: update or delete on table "Users" violates Foreign key Constraint "Tarefas_userid_fkey" on table "Tasks.

MODEL USER

class Users extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      Users.hasMany(models.Tarefas, {
        onDelete: 'CASCADE', 
        foreignKey: { allowNull: false } ,
        hooks: true
      });
    }
  };
  Users.init({
    name: DataTypes.STRING,
    sobrenome: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    typeuser: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'Users',
  });
  return Users;

MODEL TASKS

 class Tarefas extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {

      
     Tarefas.belongsTo(models.Users); 
    }
  };


  Tarefas.init({
    nome: DataTypes.STRING,
    valor: DataTypes.DECIMAL,
    local: DataTypes.STRING,
    data:DataTypes.DATE,
    status:DataTypes.REAL

  }, {
    sequelize,
    modelName: 'Tarefas',
  });
  return Tarefas;

IN MY MIGRATE TASK I ADDED

 UserId:{
        type:Sequelize.INTEGER,
        allowNull:false,
        defaultValue:0,
        references:{
          model:'Users',
          key:'id'
        }
        
      }

1 answer

-1

I solved as follows in my migrate Tasks in the attribute Userid put a property onDelete: 'CASCADE'. No matter how much you put it right into the model it doesn’t work.

Uei mysql and postgres and worked well.

UserId:{
        type:Sequelize.INTEGER,
        allowNull:false,
        defaultValue:0,
        references:{
          model:'Users',
          key:'id'
        },

        onDelete:'CASCADE'
      }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.