question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conflict with Postgresql jsonb operators

See original GitHub issue

Hi!

I have sql:

SELECT id, name, price, files->'photos'->0->>'name' as photo, annotation
FROM product
WHERE tags ?& array['17']
ORDER BY (tags->>'17')::int
LIMIT 10

If i run this sql direct in postgresql client it work fine

If i run this sql through node-postgres it work fine again:

var pg = require('pg');

var connPg = "postgres://user:password@localhost/mydbname";
var clientPg = new pg.Client(connPg);
var sqlProductsByTags = [
  "SELECT id, name, price, files->'photos'->0->>'name' as photo, annotation ",
  "FROM product ",
  "WHERE tags ?& array['17'] ",
  "ORDER BY (tags->>'17')::int ",
  "LIMIT 10 "
];

clientPg.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  //console.log('SQL = ' + sqlProductsByTags.join(''));

  clientPg.query(sqlProductsByTags.join(''), function(err, result) {
    if(err) {
      return console.error('error select products by tags', err);
    }

    for(var i = 0, max = result.rows.length; i < max; i++) {
      console.log('name: ' +  result.rows[i].name);
    }

    clientPg.end();
  });
});

If i run raw sql by knex, it fail with error (below listed):

var connPg = "postgres://user:password@localhost/mydbname"; 
var pg = require('knex')({
  client: 'pg',
  connection: connPg
});
var sqlProductsByTags = [
  "SELECT id, name, price, files->'photos'->0->>'name' as photo, annotation ",
  "FROM product ",
  "WHERE tags ?& array['17'] ",
  "ORDER BY (tags->>'17')::int ",
  "LIMIT 10 "
];

pg.raw(sqlProductsByTags.join(''))

This Error

{ [error: syntax error (at or near "$1")]
  name: 'error',
  length: 142,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '98',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'scan.l',
  line: '1053',
  routine: 'scanner_yyerror' }

I think that it fail because of i used jsonb operator ‘?&’, precise only one symbol ‘?’ Reference to PostgreSQL JSONB operators

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:33 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
elhigucommented, Aug 13, 2015

+1 for ??

1reaction
zacronoscommented, Sep 13, 2017

@SeanCannon, in my codebase I saw this working just fine over a year ago after it was fixed. Chances are very good that if you’re seeing a similar issue, it is a new problem, not a continuation of this problem.

I suggest you create a new Issue, complete with the version of knex you’re using, example code that demonstrates the problem, and output of the error and/or expected vs actual results.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL param binding conflicts with JSONB operators
PostgreSQL param binding conflicts with JSONB operators ... I'm updating a JSONB field in my postgreSQL database (using PHP Symfony DBAL) ...
Read more >
9.5: JSON Functions and Operators - PostgreSQL
The field/element/path extraction operators return the same type as their left-hand input (either json or jsonb), except for those specified as returning text, ......
Read more >
Combining Postgres jsonb query statements
The trick here is to use jsonb_set using {commentid} as the path you want to construct (second argument) and the third argument as...
Read more >
PostgreSQL JSON Functions & Operators: Types, Syntax ...
What are PostgreSQL JSON Operators? To help you query JSON data, PostgreSQL includes two native operators: -> and ->>. The JSON object field...
Read more >
PostgreSQL anti-patterns: Unnecessary json/hstore dynamic ...
PostgreSQL has json support – but you shouldn't use it for the great majority of what you're doing. This goes for hstore too,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found