Sequelize:列名小写不适用于包含的模型

Sequelize: column name to lowercase not working with included models

我有两个模型:类别和书签。书签通过 categoryId 列与类别相关联。

类别还有 namecreatedAtorderId 列;


我可以通过以下方式获取类别及其所有书签:

const category = await Category.findOne({
    where: { id: req.params.id },
    include: [
      {
        model: Bookmark,
        as: 'bookmarks'
      },
    ]
  });

但现在我想更改书签的排序方式。订单类型可以是 namecreatedAtorderId

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 为 createdAtorderId 时它工作正常但当它为 name

时失败

我收到此错误:Sequelize 生成的 SQLITE_ERROR: near \"(\": syntax error 和 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;

更改为:

const order =
    orderType == 'name'
      ? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
      : [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];

现在可以使用了。