Ruby On Rails – Rake schema – Max key length is 767 bytes Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Ruby On Rails – Rake schema – Max key length is 767 bytes without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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?

Answer

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.

We are here to answer your question about Ruby On Rails – Rake schema – Max key length is 767 bytes - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji