Wrong Escaping of array in Postgres #1733
Comments
+1, we're having this problem as well. |
Seems like array of strings is not applicable for
Produces
When running in Postgres 9.4:
|
@megavoltua what should be correct SQL in your case? Why are you using knex raw there instead of knex('docs').whereIn('type', ['account']); |
@elhigu Yes, I know I can put So I'm wondering, is there a way to use array binding for raw |
@megavoltua knex doesn't know context of SQL string where replacement is being done, so array is always replaced with '{ "item" }' format. I can think of two options if you really cannot avoid this situation (I have needed this maybe once during the last 3 years and probably it was unnecessary even then): Unroll your string array to knex.raw('SELECT * FROM docs WHERE type IN (?)', ['account']); or for bigger table something like: knex.raw('SELECT * FROM docs WHERE type IN (?,?,?)', ['account','foo','bar']); Or you can use knex.raw('SELECT * FROM docs WHERE type = ANY(?::text[])', [['account']]); |
@elhigu I think |
How to update array? |
@TangMonk What is the raw SQL query you are trying to write to make your array update? |
like this sql:
How to do this in knex? |
something like this: knex('users').update({
agents_array: knex.raw(`ARRAY[${arrayItems.map(() => '?').join(',')}]::text[]`, arrayItems)
}).then(res => console.log('results', res)); |
@elhigu thank you! |
Latest version of knex (0.12.3) does not escape array of strings correctly:
The text was updated successfully, but these errors were encountered: