Olá
Meu código ao tentar rodar uma migration para criação de uma tabela proveniente de relacionamento muitos para muitos dá o seguinte erro de saída:
ERROR: Can't create table `challenge`.`Registrations` (errno: 150 "Foreign key constraint is incorrectly formed")
As tabelas course e student são relacionadas gerando uma terceira por nome registration
Segue o código das migrations de cada uma segundo ordem de criação:
Migration course:
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Courses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
rating: {
type: Sequelize.STRING
},
diff: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Courses');
}
};
Migration course:
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Students', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
intelligence: {
type: Sequelize.STRING
},
ranking: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Students');
}
};
migration registration:
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Registrations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
grade: {
type: Sequelize.STRING
},
sat: {
type: Sequelize.STRING
},
course_id: {
type: Sequelize.INTEGER,
references: {
model: 'Course',
key: 'id'
},
onDelete: 'CASCADE',
},
student_id: {
type: Sequelize.INTEGER,
references: {
model: 'Student',
key: 'id'
},
onDelete: 'CASCADE',
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Registrations');
}
};
model course;
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Course extends Model {
static associate(models) {
Course.belongsToMany(models.Student, {
through: 'Registration',
foreignKey: 'course_id',
as: 'students'
})
}
}
Course.init({
rating: DataTypes.STRING,
diff: DataTypes.STRING
}, {
sequelize,
modelName: 'Course',
});
return Course;
};
model student:
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Student extends Model {
static associate(models) {
Student.belongsToMany(models.Course, {
through: 'Registration',
foreignKey:'student_id',
as: 'Course',
});
}
}
Student.init({
intelligence: DataTypes.STRING,
ranking: DataTypes.STRING
}, {
sequelize,
modelName: 'Student',
});
return Student;
};
model registration:
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Registration extends Model {
static associate(models) {
Registration.belongsTo(models.Course, {
foreignKey: 'course_id',
as: 'students'
});
Registration.belongsTo(models.Student, {
foreignKey: 'student_id',
as: 'course',
});
}
}
Registration.init({
grade: DataTypes.STRING,
sat: DataTypes.STRING
}, {
sequelize,
modelName: 'Registration',
});
return Registration;
};
A quem poder me ajudar, desde já agradeço muito.