question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres arrays are retrieved as objects

See original GitHub issue

I have a table (in Postgres) with a field of type TEXT[] but when I use knex (simple select query) I get that field as an object instead of an array. I read this issue but it doesn’t give an answer for that.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
richraid21commented, Oct 19, 2017

Ok so I confirmed the error, but this is actually a problem with the underlying postgres driver that Knex uses, pg. You will have to bring it up there and then Knex can upgrade when the bug gets fixed.

Heres the reproduced example using just pg driver

const { Client } = require('pg')

const config = {
    // ommitted for public post
}

/* SQL SETUP

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');

CREATE TABLE _person (
    name text,
    current_mood mood
);

INSERT INTO person VALUES ('Rich', ARRAY['happy', 'ok']::mood[]);

*/

async function go() {
    const client = new Client(config)
    
    await client.connect()
    
    const result = await client.query('SELECT * FROM _person')
    
    console.log(result.rows)
    // Returns [ anonymous { name: 'Rich', state: '{sad,happy}' } ]

    await client.end()
}

go()
1reaction
elhigucommented, Oct 19, 2017

I suppose there is nothing to do in knex side to fix this, closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.15. Arrays - PostgreSQL
First, we show how to access a single element of an array. This query retrieves the names of the employees whose pay changed...
Read more >
Postgresql array retrieved via array_to_json needs conversion ...
I am saving an array of objects in postgresql. In postgresql the field of type text[] looks like the following:
Read more >
How to Query Arrays in PostgreSQL - PopSQL
You can retrieve the contents of an array by specifying it in the select clause like any other column: select first_name,
Read more >
PostgreSQL Array: Functions, Type, Example - Guru99
The elements of the array can be retrieved using the SELECT statement. The values of the array column can be enclosed within square...
Read more >
Turning PostgreSQL rows into arrays - Reuven Lerner
So far in this series, I have described how you can create arrays and retrieve information from them — both the actual data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found