简体   繁体   English

序列化一对多查询(包含)产生与X不相关的Y

[英]Sequelize one-to-many query (with include) produces Y is not associated to X

so i'm having problems with sequelize's one to many relationship, my associations are defined like this: 所以我在sequelize的一对多关系中遇到问题,我的关联定义如下:

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

and my findAll is here: 我的findAll在这里:

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

and this is producing the error: 这会产生错误:

"error": "Y (Ys) is not associated to X!" “错误”:“ Y(Ys)与X无关!”

Not quite sure what i'm doing wrong here :/ 不太确定我在这里做错了什么:/

There's some confusion on your associations 您的协会有些困惑

logically 'X' has many 'Ys', association should be X.hasMany(Y, {as: 'Ys'}); 逻辑上,“ X”具有许多“ Y”,关联应为X.hasMany(Y, {as: 'Ys'});

'Y' belongs to 'X' should be Y.hasMany(X, {as: 'X'}); 属于“ X”的“ Y”应为Y.hasMany(X, {as: 'X'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM