0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Mysql2】Mysql2::Error: Specified key was too long; max key length is 767 bytesの初歩的な解消方法

Last updated at Posted at 2021-10-16

Mysql2に格納できる文字データは767バイトまで今はMysqlにマイグレーションできない?とのエラーが発生。
ユニコードの変更を行おうと思ったがどこのファイルに記述されているのか忘れてしまったので
備忘録として本記事を作成。

○@○-MacBook ------ % rails db:migrate
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0307s
-- add_index(:users, :email, {:unique=>true})
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Specified key was too long; max key length is 767 bytes
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Mysql2::Error: Specified key was too long; max key length is 767 bytes
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:40:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

解決策

config/datebase.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4 #←ここの記述を変更。
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock
config/datebase.yml
default: &default
  adapter: mysql2
  encoding: utf8 #←これに変更。
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock

rails db:rollbackを行い再びrails db:migrate
するとまた別のエラーが発生

○@○MacBook ------ % rails db:migrate
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Mysql2::Error: Table 'users' already exists
/Users/user/projects/furima-36905/db/migrate/20211015003423_devise_create_users.rb:5:in `change'
/Users/user/projects/furima-36905/bin/rails:9:in `<top (required)>'
/Users/user/projects/furima-36905/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Usersテーブルが既に存在しているというエラーが発生。
この事象の修正に関しては一度テーブルを削除しまた作成を行うと直ると思い
rails db:migrate:resetを実行

○@○MacBook ----- % rails db:migrate:reset
Dropped database '-----_development'
Dropped database '-----_test'
Created database '-----_development'
Created database '-----_test'
== 20211015003423 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0435s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0518s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0427s
== 20211015003423 DeviseCreateUsers: migrated (0.1382s) =======================

無事成功!

user@USER-no-MacBook ------ % rails db:migrate:status

database: ------_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20211015003423  Devise create users

rails db:migrate:statusを行い無事migrateが行われている事も確認
DB上にUsersテーブルも確認できた。

無事解決:ok_hand:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?