fireproofsocks

fireproofsocks

Ecto Schema: Enum Data Type?

Is the only way to support enum columns in an Ecto schema by using a 3rd party package like https://github.com/gjaldon/ecto_enum ?

Thanks!

Most Liked

dwahyudi

dwahyudi

I used enum in rails before, once upon a time, a co-worker just inserted a value in the middle of the enum array and it destroyed the data. :+1:

halostatue

halostatue

No. You just specify the enum column as string in the schema.

I can’t tell you exactly what the migration will look like; we have two projects where we use enums pretty heavily. The first project uses Ecto, Ecto migrations, and Ecto.Enum. The second uses Ecto, Sqitch (which uses straight-up SQL), and does not use Ecto.Enum.

Based on our experiences, I would recommend not using Ecto.Enum if you’re using PostgreSQL. There’s nothing explicitly wrong with the library itself (the documentation is a different matter), but it provides exactly two conveniences:

  1. The ability to specify enums in queries using atoms instead of binaries (:active instead of "active").

  2. The ability to create new enums without knowing the PostgreSQL ‘magic’ (which isn’t really that magic). This really isn’t that hard:

    def up do
      execute ~s"""
      IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_status') THEN
        CREATE TYPE user_status AS ENUM ('active', 'inactive');
      END IF;
      """
    end
    

    Note that you have to drop to SQL anyway to add a new value to an enum, and the Ecto.Enum documentation is flat out wrong about the syntax to use (it will cause your migrations to fail if you have to roll back at all, because there’s no possible down phase to ALTER TYPE type ADD VALUE 'value'). Instead of what the Ecto.Enum documentation recommends, use ALTER TYPE type ADD VALUE IF NOT EXISTS 'value' (I submitted this change as a PR, but the maintainer rejected this for some reason).

Qqwy

Qqwy

TypeCheck Core Team

This is the reason you should always specify the numeric values in an enum. (This is true for both Ruby and any other language with numeric enums like any C-like language)

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Other popular topics Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement