Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda