如何插入array of enums

CREATE TYPE equipment AS ENUM ('projector','PAsystem','safe','PC','phone');

然后我的桌子上有一系列设备:

CREATE TABLE lecture_room (
   id INTEGER DEFAULT NEXTVAL('lecture_id_seq')
 , seatCount int
 , equipment equipment[]
) INHERITS(venue);

以下是我try 插入的内容:

INSERT INTO lecture_room (building_code, floorNo,  roomNo, length, width
                        , seatCount, equipment) 
VALUES 
('IT', 4, 2, 10, 15 ,120, ARRAY['projector','PAsystem','safe']),

但它给了我以下错误:

ERROR: column "equipment" is of type equipment[] but expression is of type text[]
SQL state: 42804
Hint: You will need to rewrite or cast the expression.

推荐答案

PostgreSQL不知道如何将类型text的输入自动转换为类型equipment的输入.您必须显式地将字符串声明为equipment类型:

ARRAY['projector','PAsystem','safe']::equipment[]

我用SQL Fiddle证实了这一点.

Postgresql相关问答推荐

docker-compose.yml用于使用postgres的spring-boot应用程序

ST_Intersects 太慢

无法从 docker-compose 上的其他服务解析 postgres 主机名

Regexp_replace 行为会改变,如果它用空白字符串或 null 替换字符串数组中的字符串

Rails Migration 使用转换将字符串转换为整数

如何在 PostgreSQL hstore 中使用通配符查询值

在 PostgreSQL 中返回插入的行

Docker - 判断 postgres 是否准备好

brew 程序更新恼人的错误信息

为什么 PostgreSQL 将用户和组合并为角色?

Select 空字段

PostgreSQL如何连接间隔值'2天'

从 Select 中创建一个临时表,如果表已经存在则插入

Rails 4 迁移: has_and_belongs_to_many table name

将 Windows 上的 .sql 文件导入到 postgresql

PostgreSQL 字符串(255) 限制 - Rails、Ruby 和 Heroku

学习 PL/pgSQL 的好资源?

如何减少存储(缩减)我的 RDS 实例?

Postgresql 对象 ID 和元组

判断 Postgres 中是否存在序列(plpgsql)