Ruby On Rails - Rake 架构 - 最大密钥长度为 767 字节

Phi*_*lip 4 mysql schema innodb ruby-on-rails max

我尝试 rake db:schema:load 但收到错误

Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX 
Run Code Online (Sandbox Code Playgroud)

据我了解,InnoDB 索引中最多只允许 767 个字节......如果你使用 utf-8,它应该除以 3。

但是当我尝试在 schema.rb 中设置最大 100 个字符(甚至不接近 767)时,错误仍然发生......

模式.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 }
Run Code Online (Sandbox Code Playgroud)

错误

-- 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`)
Run Code Online (Sandbox Code Playgroud)

MySQL

Your MySQL connection id is 1838
Server version: 5.5.22-0ubuntu1-log (Ubuntu)
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

Mát*_*osi 5

我在您的代码中看到两个问题:

  • limit参数应该被调用length
  • 在多列索引上,length参数应该是指定两列长度的散列::length => { :slug => 200, :sluggable_type => 30 }

查看文档:http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index-label-Creating+an+index+with+specific+key+length