为什么我的序列化关联不起作用?

问题描述 投票:0回答:1

我试图在我的续集模型中添加外键,但是不知何故。这是我的学徒模型:

 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!

提前感谢

node.js database foreign-keys sequelize.js associations
1个回答
0
投票

只需将其替换为您的约会模型。您的关联有误。

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;
};
© www.soinside.com 2019 - 2024. All rights reserved.