本文介绍了导轨创造schema_migrations - Mysql2 ::错误:指定的键过长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Rails 3.2.6和MySQL 6.0.9(但我有完全在MySQL 5.2.25同样的错误)

I am using Rails 3.2.6 and Mysql 6.0.9 (but I have exactly the same error on MySQL 5.2.25)

当我创建新的数据库(耙分贝:创建),然后当我尝试加载模式(耙模式:负载)我得到这个错误:

When I create new database (rake db:create) and then when I try to load the schema (rake schema:load) I get this error:

Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)

在时间和研究时间,我发现这些解决方案:

After hours and hours of research I found these solutions:

这没有奏效。我试了一下我的Linux服务器,我的Mac上,甚至在Windows上 - 这是行不通的。

This didn't work. I tried it on my Linux server, my Mac and even on Windows - it just doesn't work.

我不需要版本列255长(当它是UTF-8,那么就需要4 * 255 = 1020字节,超过了MySQL的限制767字节键)。我并不需要它是UTF-8两种,但在数据库中的所有其他表都是UTF-8。我将utf8_czech_ci是默认排序规则。

I do not need the version column to be 255 long (when it is UTF-8, then it takes 4*255 = 1020 bytes and exceeds the MySQL limit of 767 byte for keys). I do not need it to be UTF-8 either, but all other tables in the DB are UTF-8 and I have set utf8_czech_ci to be the default collation.

这实际创建schema_migrations表的方法是这样的:

The method that actually creates the schema_migrations table looks like this:

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导轨/导轨

于是,我就添加:上限=> 100 t.column 语句,但是我没有这个解决方案成功要么。问题是,我不能让这个补丁负荷时originial已经到位。换句话说 - 我的补丁负荷的的ActiveRecord的:: SchemaMigration所以它是覆盖

So I tried to add :limit => 100 to the t.column statement, but I did not succeed with this solution either. The problem is that I cannot make this patch load when the originial is already in place. In other words - my patch loads before ActiveRecord::SchemaMigration so it is overwritten.

当我把这个配置/初始化/补丁/ 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被加载。

It is successfully loaded, but the it is overwritten when the original ActiveRecord::SchemaMigration is loaded.

我试图乱用ActiveSupport.on_load(:active_record)。但是这似乎并没有擦出火花

I tried to mess up with ActiveSupport.on_load(:active_record) but that doesn't seem to work either.

有没有办法来加载originial的ActiveRecord后,这个文件:: SchemaMigration到位,使这个补丁的工作?

你有什么建议吗?我可以澄清任何部分这个问题,如果它是没有意义的你。只是问我。我一直坚持这个太久了。

Do you have any suggestions? I can clarify any part of this question, if it makes no sense to you. Just ask me. I've been stuck with this for too long.

推荐答案

767键应该工作。请确保您使用 UTF8 编码,而不是 UTF16 。我有同样的问题,我的错误是,我无意中创建 UTF16 数据库

767 key should work. Make sure you use utf8 encoding, and not utf16. I had same problem, and my mistake was that I accidently created utf16 database

这篇关于导轨创造schema_migrations - Mysql2 ::错误:指定的键过长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:44