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 GitHub - gjaldon/ecto_enum: Ecto extension to support enums in models · GitHub ?

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)

Last Post!

fireproofsocks

fireproofsocks

Thank you all for the replies. I think you’ve convinced me to just add a separate normalized table where I can map the ids to human-readable names. That will make it easier for any BI reporting anyhow.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement