Postgres:添加ENUM值的描述?

时间:2017-02-21 12:54:51

标签: django postgresql enums

我在Postgres 9.6中有一个ENUM专栏:

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

我想为枚举中的每个值添加一个人类可读的描述,例如:对于fooThis is the foo value and it does stuff

在Postgres有没有办法做到这一点?我喜欢Django的choices field之类的东西。

2 个答案:

答案 0 :(得分:0)

我认为没什么好看的。标准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

有一些变态的幻想:

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)

答案 1 :(得分:0)

从理论上讲,您可以创建两个具有相同维度的类型,并使用以下内容:

TASK [azure : debug] ***********************************************************
task path: mytest/roles/azure/tasks/get_ip.yml:14
ok: [localhost] => {
    "azure_ip": {
        "ansible_facts": {
            "azure_publicipaddresses": [
                {
                    "etag": “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    "id": "/subscriptions/0000000000000/resourceGroups/myrgrp/providers/Microsoft.Network/publicIPAddresses/my_ip_001",
                    "location": “euwest",
                    "name": “my_ip_001",
                    "properties": {
                        "idleTimeoutInMinutes": 4,
                        "ipAddress": “20.113.125.63",
                        "ipConfiguration": {
                            "id": "/subscriptions/000000000000/resourceGroups/mygrprgrp/providers/Microsoft.Network/networkInterfaces/myrgrp-nic001/ipConfigurations/default"
                        },
                        "provisioningState": "Succeeded",
                        "publicIPAddressVersion": "IPv4",
                        "publicIPAllocationMethod": "Dynamic",
                        "resourceGuid": “fffff-4444444-cccccc"
                    },
                    "type": "Microsoft.Network/publicIPAddresses"
                }
            ]
        },
        "changed": false
    }
}