Search code examples
node.jspostgresqlpg-promise

Unparsed row in pg-promise when specifying SELECT columns


Why does pg-promise return a parsed object when querying with SELECT * ..., but returns an unparsed row when select columns are specified SELECT (given_name, family_name, photo) ...?

  • Select all columns:

    db.oneOrNone(`SELECT * FROM $1~ WHERE id=$2`,[psql.tables.user, uid]);
    > {"id":"1","given_name":"Michael","family_name":"Douglas","email":"mike.dougly@douglas.com","password":"9511393558AC7202E61EB7BA9A2D3FC1FA9CA151AFF4271C1475BD11A7BE5DC72DC8E0B2E8EA3D44DA476DB43728E1A2A4BCB08A63B2E724603B9EEBD268CE5A","birthdate":"1978-01-01T00:00:00.000Z","photo":"https://somefakewebsite.com/dougly.jpg"}
    
  • Select a subset of columns:

    db.oneOrNone(`SELECT (given_name, family_name, photo) FROM $1~ WHERE id=$2`,[psql.tables.user, uid]);
    > {"row":"(Michael,Douglas,https://somefakewebsite.com/dougly.jpg)"}
    

Is there a way to tell pg-promise to parse the column names like in the first case?


Solution

  • ...returns an unparsed row when select columns are specified

    You do not just specify columns, you place them into parenthesis, which tells PostgreSQL to return tuples, which is what you are getting. And if you run your query in pgAdmin, you will see the same.

    Is there a way to tell pg-promise to parse the column names like in the first case?

    Remove the parenthesis, and that's it.