postgres
postgres copied to clipboard
Nested arrays aren't expanded properly in 3.4.3
Using nested arrays in a where statement causes an error in
import postgres from 'postgres';
const sql = postgres('postgres:///', {
prepare: false,
});
const values = [
[]
];
const res = await sql`SELECT a, b FROM fner WHERE (a, b) IN (${sql(values)})`;
console.log(res);
This is the output from running this code where the first line is a console.log from sql on line 112 of index.js
[ 'SELECT a, b FROM fner WHERE (a, b) IN (', ')' ] [ Builder { first: [ [] ], rest: [] } ]
node:internal/process/esm_loader:40
internalBinding('errors').triggerUncaughtException(
^
TypeError: str.replace is not a function
at escape (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:217:20)
at file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:213:22
at Array.map (<anonymous>)
at escapeIdentifiers (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:213:13)
at Object.select [as fn] (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:139:12)
at Builder.build (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:71:17)
at stringifyValue (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:109:38)
at stringify (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/types.js:100:16)
at build (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:223:20)
at execute (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:167:7)
at cachedError (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:170:23)
at new Query (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:36:24)
at sql (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js:113:11)
at file:///home/admin/proto-0.14.0-747-g66d563cf/lib/build/testanotheridea.js:11:22
at cachedError (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:170:23)
at new Query (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/query.js:36:24)
at sql (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js:113:11)
This was working on version 3.3.5 and when we upgraded to 3.4.3 this error started showing up for us. Let me know how I can help.
Seems like the same issue as #701. There's a workaround there you might find useful.
Thanks for the pointer to the workaround.
Another workaround I found is running sql manually on each subarray in the 2d arr before running sql again on the result.
const arr2D = [
['baz1', 'bat1'],
['baz2', 'bat2']
]
sql`SELECT foo FROM bar WHERE (baz, bat) IN ${sql(arr2d.map((item) => sql(item)))}`
I have the same issue with the following INSERT query:
import sql from '../db.js';
import { Bar } from '../../../model/bar'
export async function createBars(bars: Bar[]) {
await sql`
INSERT INTO api.bar
(
ticker_symbol, open, high, low, close, volume, volume_weighted_average_price, timestamp
)
${sql(bars.map(value => {
return sql({
ticker_symbol: value.S,
open: value.o,
high: value.h,
low: value.l,
close: value.c,
volume: value.v,
volume_weighted_average_price: value.vw,
timestamp: value.t
})
}))}
`;
}
Using sql on each sub-item of the query doesn't work either, still throws the same str.replace is not a function error.