script

script

Using MYSQL enum in ecto migrations

I am using MYSQL instead of Postgres for my database. For Postgres it’s simple we can create a type using enum and use it in the migration to allow the values in the enum, but MYSQL doesn’t support this. Now, If I want the enum in the migrations. How can I do that with MYSQL? Let’s say this is my migration.

create table(:user) do

  add(:role, :string) # 'owner', 'worker', 'manager', 'pending_approval'
  add(:business_id, :string, null: false, default: "")
end

I only want to allow owner, worker, manager, pending approvals for role field.
Thanks.

Most Liked

script

script

I figure out a workaround for this. After creating a table with ecto migration. I executed SQL command to alter the table and add the enum column like this:

create table(:users) do
  add(:business_id, :string, null: false, default: "")
end

execute "ALTER TABLE users
   ADD role ENUM('owner', 'worker', 'manager', 'pending_approval');"

Last Post!

felix-starman

felix-starman

If someone comes across this, “quoted atoms” has been an undocumented way of supporting this and other interesting things in MariaDB/MySQL, but it’s now documented:

create table("my_table") do
  # ...
  add :my_enum, :"ENUM('value1', 'value2', 'value3')", null: false
end

This is because the migrator just passes the stringified-atom through if it’s something it doesn’t specifically handle.

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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement