在限制结果和包括相关模型时,我遇到了Sequelize的问题.
以下产生正确的结果,限制为10并正确排序.
Visit.findAll({
limit: 10,
order: 'updatedAt DESC',
}).success(function(visits) {
res.jsonp(visits);
}).failure(function(err) {
res.jsonp(err);
})
Run Code Online (Sandbox Code Playgroud)
SQL
SELECT * FROM `Visits` ORDER BY updatedAt DESC LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
但是当我添加一个关联时,它突然限制了子查询,因此排序从未发生,因为结果集有限.
Visit.findAll({
limit: 10,
order: 'updatedAt DESC',
include: [
{ model: Account, required: true }
]
}).success(function(visits) {
res.jsonp(visits);
}).failure(function(err) {
res.jsonp(err);
})
Run Code Online (Sandbox Code Playgroud)
SQL
SELECT
`Visits`.*
FROM
(SELECT
`Visits`.*, `Account`.`id` AS `Account.id`, `Account`.`email` AS `Account.email`, `Account`.`password` AS `Account.password`, `Account`.`role` AS `Account.role`, `Account`.`active` AS `Account.active`, `Account`.`createdAt` AS `Account.createdAt`, `Account`.`updatedAt` AS `Account.updatedAt`, `Account`.`practice_id` AS `Account.practice_id`
FROM …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Sequelize ORM 的功能,该功能允许从包含的模型中引用嵌套列(请参阅Sequelize 文档:顶层的复杂 where 子句)。在文档中它指出,我可以使用$nested.column$语法。
以下是我试图做的:
let where = { memberId };
if (req.query.search) {
const like = { [Op.like]: `%${req.query.search}%` };
where = {
...where,
[Op.or]: [
{ '$bookItem.serial$': like },
{ '$bookItem.book.name$': like },
{ '$bookItem.book.ISBNCode$': like },
],
};
}
const options = {
where,
include: [
{
model: models.BookItem,
as: 'bookItem',
required: false,
include: [
{
model: models.Book,
as: 'book',
attributes,
required: false,
},
],
},
],
});
const transactions …Run Code Online (Sandbox Code Playgroud)