sql - Knex 原始查询不工作 postgresql

标签 sql postgresql knex.js

我正在尝试进行以下半原始 knex 查询:

knex.raw('select sum(invoice_price * quantity) from "localhost:3000".order_item').where({cart_session: 'some_session'}).then(function(data) {
        console.log(data.rows[0].sum);
});

但是,我不断收到错误消息:knex.raw(...).where is not a function

有人可以帮忙吗?

提前致谢!

最佳答案

我认为您不能以这种方式使用 raw,它通常位于您通常会使用模式构建器的某个地方。此外,看起来您也不能在 sum 子句中执行原始操作。这似乎通过在 select 中使用 raw 来做你想做的:

knex('order_item')
  .select(knex.raw('sum(invoice_price * quantity)'))
  .where({cart_session: 'some_session'})

这会产生以下 sql:

select sum(invoice_price * quantity) from "order_item" where "cart_session" = 'some_session'

关于sql - Knex 原始查询不工作 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495379/

相关文章:

javascript - 使用 knex.transactionProvider() 时出现 KnexTimeoutError

c# - 如何使用 Npgsql 和 PostgreSQL 执行 ExecuteScalar 函数?

json - 在 postgres、Django 中保存大型 json 对象

postgresql - 查询以在亚马逊 Redshift 中获取从给定日期到 48 小时的数据

javascript - 如何为 Objection/PostgreSQL 数据库进行 'squash' knex 迁移?

mysql - knex 事务在 Nodejs 中不起作用

sql - 按列中的行号分组

sql - Access 2010 中的链接查询到受密码保护的数据库。

c++ SQL 查询构建库

sql - 如何推迟 Postgres 中的外键约束