我怎样才能将此sql转换为sequelize funciton

时间:2019-05-26 06:09:21

标签: postgresql sequelize.js

我想将sql转换为sequelize函数

我尝试过代码

const result = await models.chapter.findOne({
    logging: log.trace,
    where: {
      seq: db.fn('max', db.col('seq'))
    }
  });

SQL中的成功代码 select * from page_chapter where seq = (select max(seq) from page_chapter)

我希望得到

这样的结果
  const result = await model.chapter.findOne({
    where: {
      seq: sequelize.fn('max', sequelize.col('seq'))
    }
  })

1 个答案:

答案 0 :(得分:0)

您可以使用sequelize.literal来获取查询:

const result = await model.chapter.findOne({
    where: sequelize.literal('"seq" = (SELECT MAX("page_chapter"."seq") FROM "page_chapter")')
})