Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql dialect - errors with Array Functions #254

Open
im4LF opened this issue Aug 11, 2015 · 3 comments
Open

Postgresql dialect - errors with Array Functions #254

im4LF opened this issue Aug 11, 2015 · 3 comments

Comments

@im4LF
Copy link

im4LF commented Aug 11, 2015

Simple object with integer[] type of one field

var Table = sql.define({
        schema: 'tests',
        name: 'objects',
        columns: [
            'id', 
            'some_field' // type - integer[]
        ]
});
var query = Table.select().from(Table)
            .where(
                items.some_field.contains(sql.array(123, 456))
            )
        .toQuery();

Query generates:

{ 
    text: 'SELECT "tests"."objects".* FROM "tests"."objects" WHERE ("tests"."objects"."some_field" @> ARRAY[$1, $2])',
    values: [ 123, 456 ]
}

But when send query to postgres by

client.query(query.text, query.values)

Its generate type cast error:

error: operator does not exist: integer[] @> text[]
@im4LF
Copy link
Author

im4LF commented Sep 1, 2015

Anybody?

@stephenkubovic
Copy link

I am experiencing this as well, however I think the root cause is within the pg module, specifically this issue: brianc/node-postgres#220.

The thread suggests monkey-patching https://github.com/brianc/node-postgres/blob/715e5009dde23c0fb5b127f9b536aa6ed5245fc2/lib/utils.js#L30, I'm not sure if that's still the only solution.

@im4LF
Copy link
Author

im4LF commented Sep 15, 2015

My solution is to try guess the type by first value:

Postgres.prototype.visitArrayCall = function(arrayCall) {
  var type = 'text';
  if (arrayCall.nodes.length) {
    var test = arrayCall.nodes[0].value();
    if ('number' === typeof test)
      type = !!(test % 1) ? 'double precision' : 'integer';
  }
  var txt = 'ARRAY[' + arrayCall.nodes.map(this.visit.bind(this)).join(', ') + ']::' + type + '[]';
  return [txt];
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants