我正在使用Rails 3.2.6和Mysql 6.0.9(但我在MySQL 5.2.25上有完全相同的错误)
当我创建新数据库时(rake db:create
)然后当我尝试加载架构时(rake schema:load
)我收到此错误:
Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)
经过几个小时的研究,我找到了这些解决方案:
1.将MySQL变量innodb_large_prefix更改为true(或ON)
这不起作用。我在我的 Linux 服务器、Mac 甚至 Windows 上都试过了 - 它就是不起作用。
2. Monkeypatch ActiveRecord::SchemaMigration.create_table
我不需要version
列的长度为 255(当它是 UTF-8 时,则需要 4*255 = 1020 字节,超出了 MySQL 对键 767 字节的限制)。我也不需要它是 UTF-8,但数据库中的所有其他表都是 UTF-8,并且我已将 utf8_czech_ci 设置为默认排序规则。
实际创建 schema_migrations 表的方法如下所示:
def self.create_table
unless connection.table_exists?(table_name)
connection.create_table(table_name, :id => false) do |t|
t.column :version, :string, :null => false
end
connection.add_index table_name, :version, :unique => true, :name => index_name
end
end
您可以阅读整个文件Github 轨道/轨道 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/schema_migration.rb
所以我尝试添加:limit => 100
to the t.column
声明,但我这个解决方案也没有成功。问题是,当原始补丁已经就位时,我无法加载此补丁。换句话说 - 我的补丁加载beforeActiveRecord::SchemaMigration 因此它被覆盖。
当我把这个放进去config/initializers/patches/schema_migration.rb
:
require 'active_record/scoping/default'
require 'active_record/scoping/named'
require 'active_record/base'
module ActiveRecord
class SchemaMigration < ActiveRecord::Base
def self.create_table
unless connection.table_exists?(table_name)
connection.create_table(table_name, :id => false) do |t|
t.column :version, :string, :null => false, :limit => 100
end
connection.add_index table_name, :version, :unique => true, :name => index_name
end
end
end
end
它已成功加载,但在加载原始 ActiveRecord::SchemaMigration 时被覆盖。
我试图搞乱 ActiveSupport.on_load(:active_record) 但这似乎也不起作用。
有没有办法在原始 ActiveRecord::SchemaMigration 就位后加载此文件并使此补丁起作用?
你有什么建议吗?如果这个问题对你来说没有意义,我可以澄清它的任何部分。尽管问我。我已经被这个问题困扰太久了。