Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
Using casting (::) of array elements with pg-promise

I have a domain that represents an array of integers, created with CREATE DOMAIN x AS integer[]. I have a field in a table that has type x[], i.e. an array of arrays of integers. I have done this (as opposed to integer[][]) to allow mixed-length arrays (see this answer).

Inserting the expression {{1, 2}, {3, 4, 5}} would look like:

INSERT INTO table (column) VALUES (ARRAY[ARRAY[1, 2]::x, ARRAY[3, 4, 5]::x])

Without the domain (i.e. when column is of type integer[][]), I can use pg-promise to insert the multidimensional array above:

const arr = [[1, 2], [3, 4, 5]];
await db.query('INSERT INTO table (column) VALUES ($1)', arr);

And select it:

const result = await db.query('SELECT column FROM table');
result[0].column[1][2] // => 5

But this of course has the limitation mentioned at the start, that SQL (or at least Postgres) doesn't allow multidimensional arrays with different length rows.

Selecting a column of type x[] with pg-promise in the same way as above results in a string, and inserting a raw array (as above) understandably throws an error ("column is of type x[] but expression is of type integer[]"). Adding ::x[] (i.e. INSERT INTO table (column) VALUES ($1::x[]) also throws an error, "cannot cast type integer to x", which also make sense.

So my question is, what is the best way to select and insert fields that are arrays of a domain with pg-promise? I looked at custom type formatting, but I'm not sure that it would work for selecting. Thanks!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda