I\'m working with rails 2.3.5 application, in witch I have this field
t.string \"trip_cities\", :limit => 256
And this ind
Adding a length
option to the index in schema.rb
worked for me:
t.index ["your_index", "fields"], name: "index_your_index_fields", unique: true, length: 191
I'd noticed when migrating Rails was doing this automatically, thought this wasn't committed to the repo. I had to run rails schema:load
on another machine and it wasn't working, though adding the above solved this.
Hope that helps someone.
It sounds like the default collation uses the UTF8 character set.
MySQL limits the length of keys by bytes, not characters. Since the UTF8 implementation MySQL uses allows for 3 bytes per character, the max length of a key on a UTF8 column is 3 times the key length in characters (the key length is the full length of the field if not explicitly specified).
In this case the max key length would be 256 * 3
which is 768
. You need to either limit the length of the key or change the collation of the column.
You should recreate your database with correct options.
This helped to me with same problem
CREATE DATABASE ${DB} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
how about changing the migration itself:
remove_index "bookings", :name => :trip_cities
add_index "bookings", ["trip_cities"], :name => :trip_cities, :length => { :trip_cities => 255 }
Set a limit on the length of the string to 191 characters, example:
t.string :project, index: { unique: true }, limit: 191
OR
Use this patch: https://github.com/rails/rails/issues/9855#issuecomment-390366184