我有兩個模型:類別和書簽。書簽通過 categoryId 列與類別相關聯。
類別也有name, createdAt,orderId列;
我可以通過以下方式獲取類別及其所有書簽:
const category = await Category.findOne({
where: { id: req.params.id },
include: [
{
model: Bookmark,
as: 'bookmarks'
},
]
});
但現在我想改變書簽的排序方式。訂單型別可以是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
});
當 orderType 為createdAtor時它作業正常,但當它是時orderId失敗name
我收到此錯誤:SQLITE_ERROR: near \"(\": syntax errorSequelize 生成的 SQL 查詢是:
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;
uj5u.com熱心網友回復:
改為:
const order =
orderType == 'name'
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
現在它正在作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366675.html
