Search code examples
ruby-on-railsrubyrails-migrations

rails length of complicated index


I wrote migration like this:

create_table :table1 do |t|
  t.string :foo, null: false, default: '', limit: 512
  t.integer :bar, null: false


  t.index [:foo, :bar]
end

and got an error

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

How I can resolve this problem, except deсrease column foo limit?


Solution

  • String is varchar(255) your limit is too large so you can use t.text or change your limit to 255.

    The previous error you can fix by passing a hash linked to the field on the index.

    t.index [:foo, :bar], :length => {:foo => 25 }