Trung Tran
Trung Tran

Reputation: 13771

Knex raw query not working postgresql

I am trying to make the following semi-raw knex query:

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);
});

However, I keep getting the error: knex.raw(...).where is not a function.

Can someone help?

Thanks in advance!

Upvotes: 3

Views: 7731

Answers (1)

Gangstead
Gangstead

Reputation: 4182

I don't think you can use raw in that way, it usually goes inside someplace where you would normally use the schema builder. Also it doesn't look like you can do raw within a sum clause either. This seems to do what you want by using raw inside the select:

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

This yields the following sql:

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

Upvotes: 3

Related Questions