假设我有以下模型(一本书有多位作者,一位作者可以参与多本书):

const Book = sequelize.define('book', {...});
const Author = sequelize.define('author', {...});
Book.belongsToMany(Author, { through: BookAuthor });

我正在尝试查询 Headers 为'nodejs'的书籍,作者名称为'John Doe',限制前10个,如何完成这样的查询?

使用此处描述的方法http://docs.sequelizejs.com/manual/tutorial/models-usage.html#eager-loading似乎不适用于belongsToMany关联 .

在include部分中添加Where子句,将从主查询中过滤结果,因此它将返回前十个,在这种情况下,然后过滤掉 .

编辑:

我正在尝试的查询是:

Book.findAndCountAll({ include: [{ model: Author, where: { name: 'shakespeare' } } ], limit: 10, offset: 0 })
                     .then((results) => { console.log(results.rows.length); // -> 0, wrong 
                     console.log(results.count) // -> 2, correct 
});