【发布时间】:2019-02-26 18:28:15
【问题描述】:
我在 Rails 中有一个迁移,它执行以下操作:
class AddMissingIndexes < ActiveRecord::Migration[5.1]
def change
# Applications
add_index :applications, :evid, length: { evid: 255 }
end
end
这似乎在我的测试环境
中运行顺利但是,当我在 生产环境 中运行迁移时 我收到此错误:
Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE INDEX `index_applications_on_evid` ON `applications` (`evid`(255))
我正在尝试使用来自this question 的第二个答案来解决此问题
class AddMissingIndexes < ActiveRecord::Migration[5.1]
def change
# Applications
add_index "applications", ["evid"], :name => :evid, :length => { :evid => 255 }
end
end
但是,我想在对架构进行任何进一步更改之前确保它可以正常工作。所以我需要能够在我的测试环境中重现这个错误。
测试环境:
+-------------------------+------------------+
| Variable_name | Value |
+-------------------------+------------------+
| innodb_version | 5.5.62 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.62-0+deb8u1 |
| version_comment | (Debian) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
+-------------------------+------------------+
生产环境
+-------------------------+---------------------+
| Variable_name | Value |
+-------------------------+---------------------+
| innodb_version | 5.6.40 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.40-log |
| version_comment | Source distribution |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
+-------------------------+---------------------+
使用SHOW GLOBAL VARIABLES LIKE 'innodb_%';
我能够看到我的测试环境数据库,这就是我发现我的本地数据库环境具有以下变量:
测试环境
innodb_file_format=Barracuda;
innodb_large_prefix=1;
innodb_file_per_table=1;
innodb_file_format_max=Barracuda;
innodb_strict_mode=1;
character_set_server='utf8mb4';
生产环境
innodb_file_format=Antelope;
innodb_large_prefix=OFF;
innodb_file_per_table=ON;
innodb_file_format_max=Antelope;
innodb_strict_mode=OFF;
character_set_server='utf8mb4';
我试图重现我的生产环境,将变量一次设置为 1。但无济于事。
【问题讨论】:
-
非常简单的答案是确保您在测试和生产中具有相同的版本..
标签: mysql migration ruby-on-rails-5