Dean

Specified key was too long; max key length is 767 bytes

I’ve recently received this error when using UTF8MB4 with Rails.

Mysql2::Error: Specified key was too long; max key length is 767 bytes

After finding this issue on the Rails GitHub page, this is the fix:

Create a file called config/initializers/mysql.rb and copy the following into it

require 'active_record/connection_adapters/abstract_mysql_adapter'

module ActiveRecord
  module ConnectionAdapters
    class AbstractMysqlAdapter
      NATIVE_DATABASE_TYPES[:string] = { :name => "varchar", :limit => 191 }
    end
  end
end

Hope this helps!

Copyright 2011-2021 Dean (deanpcmad)

Buy me a Coffee