Ruby on rails: create table active_storage error Key too long


#1

In install active_storage on ruby on rails project: rails active_storage:install This command generate this migration file

This migration comes from active_storage (originally 20170806125915)

class CreateActiveStorageTables < ActiveRecord::Migration[5.2] def change create_table :active_storage_blobs do |t| t.string :key, null: false t.string :filename, null: false t.string :content_type t.text :metadata t.bigint :byte_size, null: false t.string :checksum, null: false t.datetime :created_at, null: false

  t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
  t.string     :name,     null: false
  t.references :record,   null: false, polymorphic: true, index: false
  t.references :blob,     null: false

  t.datetime :created_at, null: false

  t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end

end end

When I migrate rails db:migrate, I have this error

== 20181020210135 CreateActiveStorageTables: migrating ======================== – create_table(:active_storage_blobs) -> 0.0468s – create_table(:active_storage_attachments) rails aborted! StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: La clé est trop longue. Longueur maximale: 1000: CREATE TABLE active_storage_attachments ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(1024) NOT NULL, record_type varchar(255) NOT NULL, record_id bigint NOT NULL, blob_id bigint NOT NULL, created_at datetime NOT NULL, INDEX index_active_storage_attachments_on_blob_id ( blob_id ), UNIQUE INDEX index_active_storage_attachments_uniqueness ( record_type , record_id , name , blob_id )) C:/Sites/comptabox/db/migrate/20181020210135_create_active_storage_tables.active_storage.rb:16:in change' bin/rails:4:in require’ bin/rails:4:in `’

Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: La clé est trop longue. Longueur maximale: 1000: CREATE TABLE active_storage_attachments ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(1024) NOT NULL, record_type varchar(255) NOT NULL, record_id bigint NOT NULL, blob_id bigint NOT NULL, created_at datetime NOT NULL, INDEX index_active_storage_attachments_on_blob_id ( blob_id ), UNIQUE INDEX index_active_storage_attachments_uniqueness ( record_type , record_id , name , blob_id )) C:/Sites/comptabox/db/migrate/20181020210135_create_active_storage_tables.active_storage.rb:16:in change' bin/rails:4:in require’ bin/rails:4:in `’

The key is too long, how I can do?

Thanks a lot for your help


#2

What you see may be caused by many things such as MySQL misconfiguration, MySQL bug, or ActiveStorage/Mysql gem bug.

Check what characterset you are using in your Rails app and what’s configured as a default in MySQL. I’d stick with UTF8.

Otherwise, a very crude workaround would be to force name to the maximum length as reported by your system:

create_table :active_storage_attachments do |t|
  t.string     :name,     null: false, limit: 1000
  t.references :record,   null: false, polymorphic: true, index: false
  t.references :blob,     null: false

  t.datetime :created_at, null: false

  t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end

t.string usually has max length of 255 which may

Cheers!