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. 馃
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.
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:
The rest is handled by Postgresql.