paper_trail: Using utf8mb4 Specified key was too long; max key length is 767 bytes

I’m using utf8mb4 for my database to support emoji. Not sure if anyone might encounter the same problem illustrated below. This might be a potential issue that should be highlighted in the docs. Perhaps the migrations file should always enforce utf8 for the CHARSET option.

Using: JRuby 1.7.19 (1.9.3p551) Rails 4.2.5 Activerecord (4.2.5) paper_trail (4.0.0)

Error:

ActiveRecord::JDBCError: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes: CREATE  INDEX `index_versions_on_item_type_and_item_id` ON `versions` (`item_type`(255), `item_id`)arjdbc/jdbc/RubyJdbcConnection.java:587:in `execute'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-jdbc-adapter-1.3.19/lib/arjdbc/jdbc/adapter.rb:595:in `_execute'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-jdbc-adapter-1.3.19/lib/arjdbc/jdbc/adapter.rb:571:in `execute'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `log'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-jdbc-adapter-1.3.19/lib/arjdbc/jdbc/adapter.rb:571:in `execute'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:557:in `add_index'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:665:in `method_missing'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:634:in `say_with_time'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:634:in `say_with_time'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:654:in `method_missing'
/Users/SOHAPPY/Downloads/roy-samples/CabernetSauvignon/db/migrate/20151121144702_create_versions.rb:18:in `change'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:608:in `exec_migration'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:592:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:591:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:590:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:998:in `execute_migration_in_transaction'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:1046:in `ddl_transaction'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `(root)'
/Users/SOHAPPY/.rvm/gems/jruby-1.7.19/bin/jruby_executable_hooks:15:in `(root)'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Fix:

class CreateVersions < ActiveRecord::Migration

  # The largest text column available in all supported RDBMS is
  #1024^3 - 1 bytes, roughly one gibibyte.  We specify a size
  # so that MySQL will use `longtext` instead of `text`.  Otherwise,
  # when serializing very large objects, `text` might not be big enough.
  TEXT_BYTES = 1_073_741_823

  def change
    create_table :versions, options: 'ROW_FORMAT=DYNAMIC ENGINE=InnoDB DEFAULT CHARSET=utf8' do |t|
      t.string   :item_type, :null => false
      t.integer  :item_id,   :null => false
      t.string   :event,     :null => false
      t.string   :whodunnit
      t.text     :object,    :limit => TEXT_BYTES
      t.datetime :created_at
    end
    add_index :versions, [:item_type, :item_id]
  end
end

Note: Force CHARSET=utf8 on your versions migration created by rails g paper_trail:install

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

We’ve run into this and unless you change tables that store user input to utf8mb4, you get exceptions and/or data loss. Phones have a lot of 4byte characters easily accessible on the default keyboards, so users add smiley faces, etc. Since they are a valid part of UTF-8, they are not easy to remove before sticking data into the database.

Fix worked for me. Thanks!