我正在研究Ruby on Rails项目。
我正在使用MySQL2作为适配器。
这是迁移文件之一。
class CreatePages < ActiveRecord::Migration[6.0]
def change
create_table :pages do |t|
t.string :base_url, null: false
t.string :html, null: false
t.string :styles, null: false
t.text :images, null: false
t.timestamps
end
add_index :pages, [:base_url, :html, :styles, :images], length: { images: 767 }, unique: true
end
end
rake db:migrate
我收到此错误。
Mysql2::Error: Specified key was too long; max key length is 767 bytes
/home/ubuntu/project/db/migrate/20200504001308_create_pages.rb:12:in `change'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes
/home/ubuntu/project/db/migrate/20200504001308_create_pages.rb:12:in `change'
Caused by:
Mysql2::Error: Specified key was too long; max key length is 767 bytes
/home/ubuntu/project/db/migrate/20200504001308_create_pages.rb:12:in `change'
谁能帮我?
根据表的编码,某些字符集每个字符可以使用多个字节。utf8
MySQL中的编码要求每个字符3个字节,因此此编码的字符串限制应为255(767/3)。如果编码为utf8mb4
,则每个字符需要4个字节,则限制应设置为191(767/4)。
因此,您的迁移文件可能如下所示:
class CreatePages < ActiveRecord::Migration[6.0]
def change
create_table :pages do |t|
t.string :base_url, limit: 191, null: false
t.string :html, limit: 191, null: false
t.string :styles, limit: 191, null: false
# etc ...
end
end
end
这似乎是旧MySQL版本的限制,这就是Rails默认不处理这种情况的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句