4
5

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 5 years have passed since last update.

rake db:migrateができない件について

Last updated at Posted at 2019-09-02

#rake db:migrateができない・・・!

経緯

deviseを使用し、ログイン機能を実装中
マイグレーションファイルに、追加のカラムを記入後
rake db:migrateをターミナルで行うとエラーが発生する。

##これまでの流れ

  1. deviseをGemfileに追記し、 bundle installを実行する
  2. rails g devise:installを実行する
  3. rails g devise userを実行し、テーブルを作成する
  4. マイグレーションファイルに追加したいカラムを記入する
    image.png
  5. rake db:migrateを実行する
  6. 下記エラーが発生する
$ rake db:migrate
rake aborted!
ActiveRecord::NoDatabaseError: Unknown database 'アプリ名_development'


Caused by:
Mysql2::Error: Unknown database 'アプリ名_development'

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

データベースがないと怒られてしまった。

7. 下記を参考に、rails db:createを実行し、データベースを作成する
  https://rails-ambassador.herokuapp.com/debugs/NoDatabaseError
  https://blog.f-arts.work/archives/600

$ rails db:create
Created database 'アプリ名_development'
Created database 'アプリ名_test'

8.今度こそ!と、rake db:migrateを実行するとエラー発生

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

Mysql2::Error: Cannot add foreign key constraint: CREATE TABLE `users` (`id` int AUTO_INCREMENT PRIMARY KEY, `nickname` varchar(255) NOT NULL, `email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(255) DEFAULT '' NOT NULL, `user_image_id_id` int NOT NULL, `user_detail_id_id` int NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at` datetime, `remember_created_at` datetime, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_users_on_user_image_id_id`  (`user_image_id_id`),  INDEX `index_users_on_user_detail_id_id`  (`user_detail_id_id`), CONSTRAINT `fk_rails_938ec9574d`
FOREIGN KEY (`user_image_id_id`)
  REFERENCES `user_image_ids` (`id`)
, CONSTRAINT `fk_rails_ce7e2c90f2`
FOREIGN KEY (`user_detail_id_id`)
  REFERENCES `user_detail_ids` (`id`)
) ENGINE=InnoDB
/Users/自分/projects/アプリ名/db/migrate/20190707130850_devise_create_users.rb:5:in `change'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Cannot add foreign key constraint: CREATE TABLE `users` (`id` int AUTO_INCREMENT PRIMARY KEY, `nickname` varchar(255) NOT NULL, `email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(255) DEFAULT '' NOT NULL, `user_image_id_id` int NOT NULL, `user_detail_id_id` int NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at` datetime, `remember_created_at` datetime, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_users_on_user_image_id_id`  (`user_image_id_id`),  INDEX `index_users_on_user_detail_id_id`  (`user_detail_id_id`), CONSTRAINT `fk_rails_938ec9574d`
FOREIGN KEY (`user_image_id_id`)
  REFERENCES `user_image_ids` (`id`)
, CONSTRAINT `fk_rails_ce7e2c90f2`
FOREIGN KEY (`user_detail_id_id`)
  REFERENCES `user_detail_ids` (`id`)
) ENGINE=InnoDB
/Users/自分/projects/アプリ名/db/migrate/20190707130850_devise_create_users.rb:5:in `change'

Caused by:
Mysql2::Error: Cannot add foreign key constraint
/Users/自分/projects/アプリ名/db/migrate/20190707130850_devise_create_users.rb:5:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

外部キー設定しているけど、参照先ないよ!って怒られました・・・そりゃあそうだ・・・
外部キー設定しているカラムは、一旦削除してrake db:migrateを実行したら、無事作成できました。

== 20190707130850 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0207s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0219s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0118s
== 20190707130850 DeviseCreateUsers: migrated (0.0547s) =======================

http://localhost:3000/users/sign_in
を開くと

image.png

とりあえず、最低限のログインページはOK!

##参考
外部キー制約の書き方
https://qiita.com/ryouzi/items/2682e7e8a86fd2b1ae47

Rails「ユーザーのモデルを作成する」
https://qiita.com/macotok/items/a17a4b0d22db4e885678

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?