Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails when to use enum vs database lookup table

I'm geting towards the end of my application and have 25 lookup tables. Some of my domain classes have 15 table references as properties (one-to-one). Now I have just remembered/learned I can use enums instead. I'm sure I can refactor a bunch of those lookup tables to be enum classes. However, what are the best practices for using an enum vs. a lookup table? A few things about my application scenario that may assist in the answer:

  1. There is only one developer (me)
  2. This is a web based application
  3. It won't be a heavily used application
  4. When the view for the domain class with currently 15 lookups is rendered, all of those lookups will need to be loaded to display the data in the view. (15 joins)
  5. The lookups will be cached in memory.
  6. The lookup relationships to the domain class are all one-to-one
  7. The data in the lookups will rarely change
  8. Some of the values in the lookup tables are wordy, example: "Taking care of animals"
  9. Some of the lookups have many records. Example: one lookup is U.S. states. Another is a person's occupation.
like image 722
spock99 Avatar asked Mar 18 '26 01:03

spock99


2 Answers

I think the most obvious answer is to use Enums for things that don't change - ever. Like Days of the week, or Planets in the Solar System - ok so sometimes thnigs you thought wouldnt change actually changed, but that OK - a quick update and you are good to go for a few more years.

But when you are working with data that will certainly change over time and often enough, then by all means store that in the database. A change to a table will not require a code change and redeployment. Additionally it would be nice to add an admin interface to these - and scaffolded screens should be quick and easy enough.

like image 192
dbrin Avatar answered Mar 20 '26 00:03

dbrin


This is just my opinion. I don't think there is a "right" answer here.

If it gets stored in a database I use a lookup table. It is more flexible and based on a few things, should more performant. While you do have to do join queries, you can lessen that impact by enabling 2nd level cache. cache: true In your mappings.

One thing to remember when using an Enum instead of a lookup is that there is no longer a foreign key to a separate table so you can't guarantee the values are in the list you desire from a database level. In addition, if you need to query based on the Enum, it is now a string comparison instead of a number comparison typically.

like image 30
James Kleeh Avatar answered Mar 19 '26 23:03

James Kleeh