sql operator with array of strings

This works
const example = await db
.select({
col: sql`ARRAY['1', '2', '3']`,
})
.from(users)
.limit(1)
const example = await db
.select({
col: sql`ARRAY['1', '2', '3']`,
})
.from(users)
.limit(1)
This also works
const example = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ARRAY['1', '2']`,
})
.from(users)
.limit(1)
const example = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ARRAY['1', '2']`,
})
.from(users)
.limit(1)
This crashes
const example = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ${["1", "2"]}`,
})
.from(users)
.limit(1)
const example = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ${["1", "2"]}`,
})
.from(users)
.limit(1)
Is there any workaround to pass an array in the sql operator?
3 Replies
Gary, el Pingüino Artefacto
This is the error: PostgresError: operator does not exist: text[] @> record
Mykhailo
Mykhailo7mo ago
Hello, @Gary, el Pingüino Artefacto. You can try this
const arr = ['1', '2'];
const postgresArray = `{${arr.join(',')}}`;

const response = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ${postgresArray}`,
})
.from(tests);
const arr = ['1', '2'];
const postgresArray = `{${arr.join(',')}}`;

const response = await db
.select({
col: sql`ARRAY['1', '2', '3'] @> ${postgresArray}`,
})
.from(tests);
Gary, el Pingüino Artefacto
this one worked, thanks solo 😄
Want results from more Discord servers?
Add your server