Representing Enums in PostgreSQL
making.close.com
Representing Enums in PostgreSQL
1–10 of 76 posts
Re: Representing Enums in PostgreSQL
#2That said, they can be really nice for all the same reasons static types are nice. Good article!
Re: Representing Enums in PostgreSQL
#3They break FDW unless they are pre-created on the importing side. Super inconvenient.
Re: Representing Enums in PostgreSQL
#4Re: Representing Enums in PostgreSQL
#5I think the "right" choice for enums probably looks a little more like:
CREATE TABLE myEnum(
enumID SERIAL NOT NULL PRIMARY KEY,
enumName TEXT UNIQUE NOT NULL,
-- enum description or other metadata columns here.
);
CREATE TABLE foo(
...
associatedEnumID INTEGER NOT NULL REFERENCES myEnum(enumID),
...
);
I think this has the benefit of being space efficient like the native typed enum, while being relatively flexible (easy to change names, add new enum values, add data about the enum itself, etc.)Re: Representing Enums in PostgreSQL
#6Re: Representing Enums in PostgreSQL
#7After having suffered through the consequences of "type" enums on MySQL, and see some things go through a long life that used "enums" in the database (in multiple different databases, include Postgres), I'm not convinced that either of these are the right choice for representing enumerations. The string with check constraint seems dumb if for no other reason than if the table that uses it winds up having many rows, y…
Re: Representing Enums in PostgreSQL
#8PostgreSQL enums feel like a bit of a hack in general. I end up using an "enum table" approach in many cases as joining against a very small table has negligible performance impact in all but the most performance sensitive databases and foreign key constraints are a well understood quantity.
What they are absolutely not for is to replace the one standard way to define data enums that everybody use since relational algebra was created. It's such an outrageously naive idea that I'm sure most people here were attracted to the title thinking it's about algebraic types or some other similar misunderstanding.
Re: Representing Enums in PostgreSQL
#9smallint looks like a good alternative, with dictionary in the app or separate table. So far i've only seen storing dictionary in app source code approach
Right?
Re: Representing Enums in PostgreSQL
#10I usually use C# Enums translated by EF Core now, which works perfectly fine on the C# side. The only missing part would be to give the DB the information about the enum names, so it could show those to me instead of just the raw numbers. But I assume there is no way to do that. Adding entire tables just doesn't seem worth it for this use case alone.