Search code examples
mysqlruby-on-railsrubyenumsrails-migrations

Problem when adding a MySQL enum column via ruby Rails migration


I'm trying to add a MySQL enum column by doing:

# frozen_string_literal: true
class AddStatusToTasks < ActiveRecord::Migration[6.0]
  def up
    execute <<-SQL
        ALTER TABLE tasks ADD status enum('to_do', 'doing', 'done');
    SQL
  end
  def down
    remove_column :tasks, :status
  end
end

But I get the error:

AddStatusToTasks: migrating =================================
-- execute("        ALTER TABLE tasks ADD status enum('to_do', 'doing', 'done');\n")
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: near "'to_do'": syntax error

My model likes looks like:

class Task < ApplicationRecord
  belongs_to :user
  enum status: { to_do: 'to_do', doing: 'doing', done: 'done' }
end

I'm actually following the guide here, but I can't find the problem.

Thanks everybody in advance for the help!


Solution

  • SQLite3::SQLException -- you are executing this query on a SQLite database, not MySQL. Are you using the correct environment?