简体   繁体   English

当类型为 int 时,Postgres 枚举不起作用

[英]Postgres Enums not working when type is int

I am trying to create an enum like below我正在尝试创建一个如下所示的枚举

create type oddNum as enum (1,3,5,7)

However I am getting the below error.但是我收到以下错误。

ERROR: syntax error at or near "1" LINE 1: create type oddNum as enum (1,3,5,7)错误:“1”处或附近的语法错误 第 1 行:将类型 oddNum 创建为枚举 (1,3,5,7)

Postgres documentation says nothing about enum typing being restricted to varchar. Postgres 文档没有提到将枚举类型限制为 varchar。 What is the problem here, and why is it not letting me create this enum.这里有什么问题,为什么不让我创建这个枚举。

As documented in the manual the elements of an enum can only be strings. 如手册中所述,枚举的元素只能是字符串。

The hint is this part:提示是这部分:

CREATE TYPE name AS ENUM
    ( [ 'label' [, ... ] ] )

Note the 'label' which clearly identifies a string in SQL.请注意'label' ,它清楚地标识了 SQL 中的一个字符串。


If you want to restrict values of a column to odd integers only (maybe in a certain range), a check constraint would be a better option.如果您想将列的值限制为仅奇数(可能在某个范围内),检查约束将是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM