What is better: a lookup table or an enum type?

(cybertec-postgresql.com)

28 points | by todsacerdoti 7 hours ago

4 comments

  • systems 4 hours ago
    well uniformity and homoiconicity are very important in an ideal db management system (a.k.a a true rdbms) everything should be represent as a relation and use the same set of operators to be manipulated

    separations of types and relations should be limited to core atomic type, string, int , date etc ... (althought date is debatable as is not usually atomic in most cases, and many dbs end up with one more date relations)

    anyway, always use a table .. when its a choice

    • netcraft 4 hours ago
      couldn't have said it better myself.

      Data should be data, queryable, relational. So often I have had to change enums into lookup tables - or worse, duplicate them into lookup tables - because now we need other information attached to the values. Labels, descriptions, colors, etc.

      My biggest recommendation though is that if you have a lookup table like this, make the value you would have made an enum not just unique, but _the primary key_. Now all the places that you would be putting an ID have the value just like they would with an enum, and oftentimes you wont need to join. The FK makes sure its valid. The other information is a join away if you need it.

      I do wish though that there were more ways to denote certain tables as configuration data vs domain data, besides naming conventions or schemas.

      Edit to add: I will say there is one places where I have begrudgingly used enums and thats where we have used something like prisma to get typescript types from the schema. It is useful to have types generated for these values. Of course you can do your own generation of those values based on data, but there is a fundamental difference there between "schema" and "data".

      • systems 3 hours ago
        well, if DDL (data definition language) and DML (data manipulation language), were unified and both operated on relation , manipulating meta data would have been a lot simpler, and more dynamics

        you can always created data dictionary relation, where you stored the code for table creation, add meta data, and use dynamic sql to execute the DML code stored in the DB, i worked somewhere where they did this ... sort of

    • 9rx 2 hours ago
      > everything should be represent as a relation

      > always use a table .. when its a choice

      Everything should be represented as relations (sets of tuples) but you should always use tables (multisets of tuples) when possible? That seems a little contradictory.

      • systems 1 hour ago
        how do you want to represent relations in a DBMS, an enum or a table ?
    • mamcx 4 hours ago
      Yeah, that is what I think on https://tablam.org, where I consider everything could be a relation, so like

          "hello world" ? where #chars != " " == ["h", "e", ...]
  • CuriouslyC 4 hours ago
    From a maintainability standpoint lookup tables are miles ahead, but from a DX perspective there are a few cases where enums are nice. Honestly I probably would never use enums again, I feel like it's caused pain every time I've done it.
    • tucnak 15 minutes ago
      Enums are great if you're into json/jsonb custom logic and aggregates. It's quite cool to use the constraint system to impose checks on various JSON fields, especially if you're doing extension development, or packaging up procedures for downstream consumption.
  • aksss 1 hour ago
    Table with a thread-safe read-through cache in code, imo. But there are places where enums make sense. For instance, things that are specifically in the code's domain.
  • sublinear 5 hours ago
    Basically ugly no matter what.

    In a lot of web apps this need tends to be related to validation, so many just do these lookups and simple comparisons in their app logic and based on static values from config files long before any db query is made. Sometimes you just don't need to involve the database and the performance would be better for it anyway.