我试图在我的续集模型中添加外键,但是不知何故。这是我的学徒模型:
module.exports = (sequelize, DataTypes) => {
const Apprentice = sequelize.define('Apprentice', {
id: {
primaryKey: true,
type: DataTypes.STRING
},
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
ghr: DataTypes.STRING,
qpa: DataTypes.STRING,
job: DataTypes.STRING,
lj: DataTypes.INTEGER,
group_id: DataTypes.INTEGER,
time_to_travel: DataTypes.STRING,
role: DataTypes.STRING,
employment_area: DataTypes.STRING,
active: DataTypes.BOOLEAN
}, {});
Apprentice.associate = function(models) {
Apprentice.hasMany(models.Appointment, {foreignKey: 'apprentice_id',sourceKey: 'id'});
};
return Apprentice;
};
这是我的约会模特:
module.exports = (sequelize, DataTypes) => {
const Appointment = sequelize.define('Appointment', {
title: DataTypes.STRING,
start: DataTypes.STRING,
end: DataTypes.STRING,
description: DataTypes.STRING,
type: DataTypes.STRING,
apprentice_id: DataTypes.INTEGER,
series_appointment: DataTypes.STRING
}, {});
Appointment.associate = function(models) {
models.Appointment.belongsTo(models.Apprentice, {foreignKey: 'apprentice_id'});
};
return Appointment;
};
我看过续集纪录片,但没有发现任何帮助。
错误:
[SequelizeEagerLoadingError]: Apprentice is not associated to Appointment!
提前感谢
只需将其替换为您的约会模型。您的关联有误。
module.exports = (sequelize, DataTypes) => {
const Appointment = sequelize.define('Appointment', {
title: DataTypes.STRING,
start: DataTypes.STRING,
end: DataTypes.STRING,
description: DataTypes.STRING,
type: DataTypes.STRING,
apprentice_id: DataTypes.INTEGER,
series_appointment: DataTypes.STRING
}, {});
Appointment.associate = function (models) {
models.Appointment.belongsTo(models.Apprentice, { foreignKey: 'apprentice_id' });
};
return Appointment;
};