node.js - 枚举类型的数组作为字符串从 Postgres 返回

标签 node.js postgresql node-postgres

给定一个自定义枚举: CREATE TYPE vehicle AS ENUM(“汽车”、“卡车”、“自行车”);

像这样的表格:

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

当像这样从我们的 Node 应用程序查询时:

SELECT * FROM vehicle_events;

这会返回一个像这样的 json 结构:

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

我的问题确实是,为什么标签数组作为字符串返回(另请注意基类型的提及数组不是)?这是因为它是一个 ENUM 数组吗?如果是这样,我怎样才能将它强制为常规数组?为什么 postgres 以这种方式返回它?

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

最佳答案

这就是 postgres 数组在不被解释为数组时的样子。如果您在查询中使用 to_json() ,数组将适本地返回。

喜欢:SELECT to_json(labels) as labels FROM vehicle_events;

关于node.js - 枚举类型的数组作为字符串从 Postgres 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644522/

相关文章:

Node.js - 在 mysql 池连接中使用等待新 Promise - SyntaxError : Unexpected token new

postgresql - 在数据库迁移期间对表进行 ALTER 编辑后,我可以返回 'recompile' 表函数吗?

javascript - postgres : relation does not exist when the table clearly exists when calling from node. js

javascript - 与nodejs程序连接时出现错误: connect ECONNREFUSED 127. 0.0.1:5432

sql - 获取给定时间戳和 TZ 名称的 TZ 缩写

postgresql - 在 pg 转储后插入到表中

javascript - 无法获取 promise 函数的原始返回值

node.js - 在 FreeBSD 10.1 上 npm 安装 Canvas

javascript - Node Selenium WebdriverJS - 如何检查元素是否真正可点击?

sql - 如果表只包含日期时间字段,在 Postgres 中是否可以检索不同的日期数据?