1
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.

devise導入後migrateをしたらmax key length is 767 bytesエラーが。

Posted at

#今回のエラー

ユーザー認証のdeviseを導入しようと

rails db:migrate

を実行したらこんなエラーが

== 20200809082251 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0154s
-- 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
.
.
.
.
.

#解決方法

###1.mysql.rbを新規作成

https://qiita.com/terufumi1122/items/9ea764618eba01144e09
こちらのQiitaの記事を参考にmysql.rbを新規作成して下記のように記述

config/initializer/mysql.rb
require 'active_record/connection_adapters/abstract_mysql_adapter'

module ActiveRecord
  module ConnectionAdapters
    class AbstractMysqlAdapter
      NATIVE_DATABASE_TYPES[:string] = { :name => "varchar", :limit => 191 }
    end
  end
end

###2.データベースをリセット&migrate実行
この状態でrails db:migrateを実行すると

== 20200809082251 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'users' already exists
.
.
.

userテーブルはすでにありますよ!と言われてしまうため一度データベースをリセットして再度rails db:migrateを実行する必要がある。リセットをマイグレーション実行を同時に行えるのが下記のコマンド

rails db:migrate:reset

こちらを実行すると

Dropped database 'training_app_development'
Dropped database 'training_app_test'
Created database 'training_app_development'
Created database 'training_app_test'
== 20200809082251 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0110s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0093s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0070s
== 20200809082251 DeviseCreateUsers: migrated (0.0274s) =======================

見事、deviseの設定をusersテーブルに反映する事ができた

1
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
1
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?