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