Sequelize 一对多查询(包含)产生 Y is not associated to X

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

所以我在使用 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) is not associated to X!"

不太确定我在这里做错了什么:/

你的联想有些混乱

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

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