seanmor5
Author of Genetic Algorithms in Elixir
This is just a quick database design discussion. When you encounter a field in a database that can take on one of a set number of values, how do you handle storing them?
Let me give an example: A database that has a table for cars stores the condition the car is in. Say the options are: “New”, “Excellent”, “Good”, “Average”, and “Bad”.
The way I see it there are 3 options here in storing these conditions:
- Create a separate “condition” table with a relationship between that and the car table. This means you can associate more information with the condition.
- Store the condition in an enumerable type with the ecto enum library. Postgres handles the validation and such for you.
- Store the condition as a string and do all validation and such on the server side of things. This might be the simplest, but it’s a lot less flexible.
What do you all think is the best approach and why? Under what conditions would you choose one approach over another?
Trending in Discussions
Hey folks,
I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Hi everyone,
I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
A note on form. Joe Armstrong, co-creator of the Erlang programming language, died in April 2019. What follows is a work of narrative non...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
I usually go with
1.I think all approaches are more or less equivalent … I’ve found
1.a bit easier to manage than enums.cnck1387
Depends a lot on the use case. For things that will very likely not change enums are useful.
For example if you have a
discountschema and one of the fields isvalue_typewhich can either befixedorpercentthen an enum fits here very well. The ecto enum lib is quite nice for this because you can reference your values as:fixedor:percentin the code base, so it retains readability.idi527
It’s possible with approach
1.as well by providing a custom ecto type for the fieldThe code for
CarConditionwould be the same as for an enum except fortype/0callback (:stringinstead of enum name), so ecto enum could probably be used as well.al2o3cr
Some questions that help me decide tables vs enums:
Does the code care about the values?
a type like “Car Condition” might only be used mostly for filtering / sorting. Code generally only interacts with values of this type in aggregate (“show a list of all conditions”) or from user input (“show cars with the car condition in
params[:condition]”). Tables work great for this.on the other hand, a type like “Order Status” that represents an order’s flow through a series of processes is used more specifically. Some code still uses values of this type for filtering and sorting, but code also refers to specific values of the type (“do X if the order status is
new”). An enum makes a lot of sense here.What does adding new values to the list mean?
What values need to be available during tests?
As with everything, there’s a lot of fuzziness - for instance, what if there’s a need to calculate an “average” car condition?
Approach 3 can be useful for short periods - for instance, if you’re prototyping a system and discovering which values should exist. Long-term, it can be hazardous to legacy data integrity.
Other approaches worth considering:
map the values to an integer for storage; this requires some discipline to not reuse values long-term. If you pick the right order - “New” => 5, “Excellent” => 4, etc in the car example - things like “average condition” are readily computable. Making an Ecto type should wrap that up neatly.
in the enum case, consider a table of “extra data” with a primary key of the enum type - that would allow using a standard
belongs_toto fetch the “extra data” for a recordOvermindDL1
Then I use an enumeration type, none of those 3 choices/options are required then.