求助-续集-ORM-关联无效 [英] SOLVED - Sequelize - ORM - Associations not working

查看:89
本文介绍了求助-续集-ORM-关联无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中使用Sequelize.我有三种模式.顾问,家庭成员和任命.任命是指顾问和家庭成员.

Using Sequelize with MySQL. I have three models. Consultant, FamilyMember and Appointments. Appointment refers to Consultant and FamilyMember.

我已经在约会模型中定义了外键.创建数据库时-外键可见-当我通过My​​SQL客户端在约会表上进行检查时.表名被冻结-因此,表名没有任何复数的机会.

I have defined the foreign keys in the Appointment model. When the DB is created - the foreign keys are visible - when I check through a MySQL client, on the appointment table. The table names are freeze - so there isn't any chance of pluralization of the table names.

顾问模型:

module.exports = (sequelize, DataTypes) => {
const consultant = sequelize.define('consultant', {
  ID: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false
  },
  FirstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  LastName: {
    type: DataTypes.STRING,
    allowNull: false
  }
{
  freezeTableName: true 
}
);

return consultant;
};

约会模式:

module.exports = (sequelize, DataTypes) => {
const appointment = sequelize.define('appointment', {
  // attributes
  ID: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false
  },
  ConsultantID: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {         
      model: 'consultant',
      key: 'ID'
    }
  },
  FamilyMemberID: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {         
      model: 'familymember',
      key: 'ID'
    }
  }
}, 
{
  freezeTableName: true 
}
);

appointment.associate = function (models) {
  models.appointment.belongsTo(models.consultant, {
    foreignKey: 'ConsultantID',
    as: 'consultant',
  });
  models.appointment.belongsTo(models.familymember, {
    foreignKey: 'FamilyMemberID',
    as: 'familymember',
  });
};

return appointment;
};

家庭成员模型:

module.exports = (sequelize, DataTypes) => {
const familymember = sequelize.define('familymember', {
  // attributes
  ID: {
    primaryKey: true,
    type: DataTypes.UUID,
    allowNull: false
  },
  FamilyID: {
    type: DataTypes.UUID,
    allowNull: false
  },
  FirstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  LastName: {
    type: DataTypes.STRING,
    allowNull: false
  }
}, 
{
  freezeTableName: true 
}
);
return familymember;
};

然后在代码中,我尝试获取约会并像这样获得相关的家庭成员和顾问

Then in the code I try to fetch appointment and get the related familymember and consultant like this

    var appointments = await Appointment.findAll({
        where: {
            AppointmentDateConfirmed: {
                $gte: moment().subtract(0, 'days').toDate()
              }
        }, include:[Consultant, FamilyMember]
    }
    )

但是我得到一个错误

UnhandledPromiseRejectionWarning:SequelizeEagerLoadingError:顾问与约会无关!

推荐答案

我想您应该在模型注册后注册关联,就像我在此

I suppose you should register your associations after models registration like I pointed in this answer

这篇关于求助-续集-ORM-关联无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆