Pg: Enum for PostgreSQL

Created on 2 Oct 2018  路  3Comments  路  Source: go-pg/pg

Hi. How can I have an ENUM field type in my model? I've searched the wiki and the docs but got nothing.

Example model:

type MyModel struct {
    ID        int64
    Name string    `sql:",notnull"`
    Type // ENUM with (x, y, z) as valid values?
}

I think it should be documented too. 馃

Most helpful comment

There is nothing smart/useful go-pg can do with enums - PostgreSQL server does all required work. Just declare your enum type and start using it:

CREATE TYPE my_enum AS ENUM ('x', 'y', 'z');

type MyModel struct {
    ID         int64
    Name string    `sql:",notnull"`
    Type string `sql:"type:my_enum"`
}

The rest is handled by Postgresql.

All 3 comments

There is nothing smart/useful go-pg can do with enums - PostgreSQL server does all required work. Just declare your enum type and start using it:

CREATE TYPE my_enum AS ENUM ('x', 'y', 'z');

type MyModel struct {
    ID         int64
    Name string    `sql:",notnull"`
    Type string `sql:"type:my_enum"`
}

The rest is handled by Postgresql.

@vmihailenco Thanks! I get it! 馃檹
How about supporting more tags in CreateTable of DB type? What's your opinion about it? I mean, we can create the enum type by reading some other tags in model definitions. 馃

It is possible, but I don't think that is a good idea. Just using SQL in such rare cases like creating enums is enough and results in more readable/predictable code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AbooJan picture AbooJan  路  5Comments

Alireza-Ta picture Alireza-Ta  路  3Comments

m-kad picture m-kad  路  3Comments

noggan picture noggan  路  5Comments

bobobo1618 picture bobobo1618  路  3Comments