【问题标题】:Sequelize one-to-many query (with include) produces Y is not associated to XSequelize 一对多查询(包含)产生 Y 不与 X 关联
【发布时间】:2017-06-05 19:05:49
【问题描述】:

所以我在使用 sequelize 的一对多关系时遇到问题,我的关联定义如下:

X.hasMany(Y, { as: 'Ys' });
Y.belongsTo(X, { as: 'X' });

我的 findAll 在这里:

return X.findAll(
        {
            where: {
                something: something,
            },
            include: [{ model: db.Y, as: 'Ys' }]
        }
    );

这会产生错误:

"error": "Y(Ys) 没有关联到 X!"

不太清楚我在这里做错了什么:/

【问题讨论】:

    标签: node.js orm sequelize.js one-to-many


    【解决方案1】:

    你的联想有些混乱

    逻辑上'X'有很多'Y',关联应该是X.hasMany(Y, {as: 'Ys'});

    'Y'属于'X'应该是Y.hasMany(X, {as: 'X'});

    【讨论】:

    • 它实际上解决了我的问题,谢谢,我会将其标记为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多