2017-02-21 7 views
1

私はPostgresの9.6でENUMコラム持っている:私は、例えば、列挙型の各値について、人間が読める記述を追加したいPostgres:ENUM値の説明を追加しますか?

CREATE TYPE my_type AS ENUM('foo', 'bar'); 

fooの場合、This is the foo value and it does stuff

これをPostgresで行う方法はありますか?私はDjangoのchoices fieldのようなものが好きです。

答えて

0

私は思います。あなたは同じ次元の2種類を作成し、このようなものを使用することができます

t=# comment on type my_type is '{"foo": "something fooish", "bar": "a place to avoid"}'; 
COMMENT 
t=# select pg_catalog.obj_description(t.oid, 'pg_type')::json->>'foo' from pg_type t where typname = 'my_type'; 
    ?column? 
------------------ 
something fooish 
(1 row) 
0

Teoretically:

CREATE TYPE my_type AS ENUM('foo', 'bar'); 
CREATE TYPE my_type_description AS ENUM('foo desc', 'bar desc'); 

CREATE FUNCTION show_desc(i my_type) RETURNS my_type_description AS $sql$ 
    SELECT ((enum_range(NULL::my_type_description))[array_length(enum_range(NULL, i), 1)])::my_type_description; 
$sql$ LANGUAGE SQL STABLE; 

SELECT show_desc('foo'); 

show_desc 
----------- 
foo desc 
(1 row) 

SELECT show_desc('bar'); 

show_desc 
----------- 
bar desc 
(1 row) 
をいくつかの変態ファンタジーと
t=# \x 
Expanded display is on. 
t=# comment on type my_type is 'foo: something fooish, bar: a place to avoid'; 
COMMENT 
t=# \dT+ my_type 
List of data types 
-[ RECORD 1 ]-----+--------------------------------------------- 
Schema   | public 
Name    | my_type 
Internal name  | my_type 
Size    | 4 
Elements   | foo           + 
        | bar 
Owner    | postgres 
Access privileges | 
Description  | foo: something fooish, bar: a place to avoid 

標準 comment ..?
関連する問題