Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails迁移-Mysql2::错误:指定的键太长;最大密钥长度为767字节_Ruby On Rails_Ruby_Database_Migration_Mysql2 - Fatal编程技术网

Ruby on rails Rails迁移-Mysql2::错误:指定的键太长;最大密钥长度为767字节

Ruby on rails Rails迁移-Mysql2::错误:指定的键太长;最大密钥长度为767字节,ruby-on-rails,ruby,database,migration,mysql2,Ruby On Rails,Ruby,Database,Migration,Mysql2,我正在从事RubyonRails项目 我使用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

我正在从事RubyonRails项目

我使用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
我得到了这个错误

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'

有人能帮我吗?

根据您的表编码,某些字符集每个字符可以使用多个字节。MySQL中的
utf8
编码要求每个字符有3个字节,因此此编码的字符串限制应为255(767/3)。如果编码为utf8mb4(每个字符需要4个字节),则限制应设置为191(767/4)

因此,您的迁移文件可能如下所示:

class CreatePages
这似乎是旧版本MySQL的一个限制,这就是Rails默认情况下的原因

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'