gpt4 book ai didi

javascript - FeathersJS Sequelize - 关联不起作用

转载 作者:行者123 更新时间:2023-11-30 21:10:19 24 4
gpt4 key购买 nike

我目前正在使用 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],
(...)
}
(...)
};

您需要为查找/获取以及您希望返回关联数据的任何其他对象执行此操作。

还有 fastJoinpopulate在 feathers-hooks-common 中。

关于javascript - FeathersJS Sequelize - 关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com