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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









