3

Im trying to rake db:schema:load but I get the error

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

From what I understand is that InnoDB only allows a max of 767 bytes in their index... And if you're using utf-8 it should be divided by 3.

But when i try to set a maximum of 100 characters (its not even close to 767) in the schema.rb the error still occurs...

schema.rb

add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true, :length => { :name => 100, :slug => 100, :sluggable_type => 40 }

Error

-- add_index("friendly_id_slugs", ["slug", "sluggable_type"], {:name=>"index_friendly_id_slugs_on_slug_and_sluggable_type", :unique=>true, :length=>{:name=>100, :slug=>100, :sluggable_type=>40}})
rake aborted!
Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `index_friendly_id_slugs_on_slug_and_sluggable_type` ON `friendly_id_slugs` (`slug`, `sluggable_type`)

MySQL

Your MySQL connection id is 1838
Server version: 5.5.22-0ubuntu1-log (Ubuntu)

What am I missing?

4

2 回答 2

4

I see two problems in your code:

  • The limit parameter should be called length
  • On multi-column indexes the length parameter should be a hash that specifies the length for both columns: :length => { :slug => 200, :sluggable_type => 30 }

Check out the documentation: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index-label-Creating+an+index+with+specific+key+length.

于 2012-06-07T12:10:08.513 回答
-1

if you are using mysql 6, one utf char can be 4 (!) bytes.

but more importantly i do not think add_index supports :limit. It does however support :length for column-wise char lengths limits.

于 2012-06-07T12:22:21.623 回答