【问题标题】:Fetch parent record from children using sequelize使用 sequelize 从孩子那里获取父记录
【发布时间】:2020-11-06 15:15:38
【问题描述】:

我有 2 个表任务和组,关联为

//Groups有很多任务

        db.groups.hasMany(db.tasks, { as: "tasks" });
        db.tasks.belongsTo(db.groups, {
        foreignKey: "groupId",
         as: "group",
        });


I have tried this but no luck,
        await Task.findAll({where: { userId: id },
        include: [
        { model: db.groups, as: "groups" },
        ],
        });

如何使用 sequelize 从任务中检索组?简单地说,我想使用 sequelize 从孩子那里获取父记录。

提前致谢。

【问题讨论】:

    标签: mysql node.js sequelize.js


    【解决方案1】:

    你几乎实现了你的目标。只需更正 include 选项中的别名:

    await Task.findAll({where: { userId: id },
      include: [
       { model: db.groups, as: "group" },
      ],
    );
    

    【讨论】:

    • 糟糕。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多