我目前正在使用 Sequelize =>4.0,发现将 2 个模型链接在一起并不像我想象的那样有效。
基本上,我有 2 个模型:user 和 punch。
用户: ID 姓名 邮箱
冲床: ID 用户 ID <- 用户 ID 时间 状态
我想显示用户列表及其链接。
这是我在每个模型下的 .associate 函数:
users.associate = function (models) { // eslint-disable-line no-unused-vars
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
users.hasMany( models.punch, {foreignKey: 'id'} );
};
punch.associate = function (models) { // eslint-disable-line no-unused-vars
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
punch.belongsTo(models.users, {foreignKey: 'userId', targetKey: 'id'});
};
这是正确的方法吗?还有什么我需要做的吗?当我查看用户 GET 请求时,除了用户数据,我没有看到任何其他内容。
任何帮助将不胜感激:)
最佳答案
您需要根据 feathers-sequelize docs 更新您的 before Hook :
// users.hooks.js
associationHook(context){
const PunchModel = context.app.services.punch.Model;
context.params.sequelize = {
include: [{ model: PunchModel }]
}
}
module.exports = {
before: {
(...)
find: [associationHook],
(...)
}
(...)
};
您需要为查找/获取以及您希望返回关联数据的任何其他对象执行此操作。
关于javascript - FeathersJS Sequelize - 关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216581/