[英]Sequelize: column name to lowercase not working with included models
I have two models: Category and Bookmark.我有两个模型:类别和书签。 Bookmark is associated with category through categoryId column.书签通过 categoryId 列与类别关联。
Category also have name
, createdAt
, orderId
columns; Category 也有name
、 createdAt
、 orderId
列;
I can get category and all its bookmarks with:我可以通过以下方式获取类别及其所有书签:
const category = await Category.findOne({
where: { id: req.params.id },
include: [
{
model: Bookmark,
as: 'bookmarks'
},
]
});
But now I want to change how bookmarks are ordered.但现在我想更改书签的排序方式。 Order type can be name
, createdAt
or orderId
;订单类型可以是name
、 createdAt
或orderId
;
const order = orderType == 'name'
? [[
{ model: Bookmark, as: 'bookmarks' },
Sequelize.fn('lower', Sequelize.col('bookmarks.name')),
'ASC'
]]
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']]
const category = await Category.findOne({
where: { id: req.params.id },
include: [
{
model: Bookmark,
as: 'bookmarks'
},
],
order
});
It works fine when orderType is createdAt
or orderId
but fails when it's name
当 orderType 是createdAt
或orderId
时它工作正常,但它的name
时失败
I'm getting this error: SQLITE_ERROR: near \"(\": syntax error
and SQL query generated by Sequelize is:我收到此错误: SQLITE_ERROR: near \"(\": syntax error
and SQL 由 Sequelize 生成的查询是:
SELECT `Category`.`id`, `Category`.`name`, `bookmarks`.`id` AS `bookmarks.id`, `bookmarks`.`name` AS `bookmarks.name`, `bookmarks`.`categoryId` AS `bookmarks.categoryId`, `bookmarks`.`orderId` AS `bookmarks.orderId`, `bookmarks`.`createdAt` AS `bookmarks.createdAt`
FROM `categories` AS `Category`
INNER JOIN `bookmarks` AS `bookmarks` ON `Category`.`id` = `bookmarks`.`categoryId`
ORDER BY `bookmarks`.lower(`bookmarks`.`name`) ASC;
Changed it to:将其更改为:
const order =
orderType == 'name'
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
and now it's working.现在它正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.