Ric*_*ard 8 django postgresql enums
我在 Postgres 9.6 中有一篇ENUM专栏:
CREATE TYPE my_type AS ENUM('foo', 'bar');
Run Code Online (Sandbox Code Playgroud)
我想为枚举中的每个值添加人类可读的描述,例如 for foo, This is the foo value and it does stuff。
在 Postgres 中有什么办法可以做到这一点吗?我想要类似 Django's choice field的东西。
我认为没有什么花哨的事情可以做。标准comment?..
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
Run Code Online (Sandbox Code Playgroud)
一些书呆子的狙击:
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)
Run Code Online (Sandbox Code Playgroud)