Search code examples
node.jspostgresqlnode-postgres

Array of type enum coming back as string from Postgres


Given a custom enum: CREATE TYPE vehicle AS ENUM ('car', 'truck', 'bicycle');

And a table like so:

CREATE TABLE vehicle_events (
  timestamp timestamptz NOT NULL DEFAULT current_timestamp,
  labels vehicle[] NOT NULL,
  mentions int4[] NOT NULL DEFAULT '{}'
);

When this is queried from our node application like so:

SELECT * FROM vehicle_events;

This returns a json structure like so:

[{"timestamp": "January, 06 2016 23:04:56", "labels": "{'car'}", "mentions": [1,2,3,4]}]

My question really is, why is the labels array coming back as a string(note also that the mentions array of a base type is not)? Is this because it is an array of an ENUM? If so, how can I force it into a regular array? And why is postgres returning it in this manner?

sqlfiddle link: http://sqlfiddle.com/#!15/637ce/1


Solution

  • That is how postgres arrays look if they aren't being interpreted as arrays. If you use to_json() in the query, the array will return appropriately.

    Like: SELECT to_json(labels) as labels FROM vehicle_events;