Mysql::Error: Specified key was too long; max key length is 767 bytes: CREATE INDEX

后端 未结 5 1509
时光说笑
时光说笑 2020-12-06 05:48

I\'m working with rails 2.3.5 application, in witch I have this field

t.string   \"trip_cities\",             :limit => 256

And this ind

相关标签:
5条回答
  • 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.

    0 讨论(0)
  • 2020-12-06 06:00

    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.

    0 讨论(0)
  • 2020-12-06 06:16

    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;
    
    0 讨论(0)
  • 2020-12-06 06:19

    how about changing the migration itself:

    remove_index "bookings", :name => :trip_cities
    add_index "bookings", ["trip_cities"], :name => :trip_cities, :length => { :trip_cities => 255 }
    
    0 讨论(0)
  • 2020-12-06 06:19

    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

    0 讨论(0)
提交回复
热议问题