1
2

More than 3 years have passed since last update.

Mysql2::Error: Unknown table '~': DROP TABLE `~`の対処方法

Last updated at Posted at 2020-12-01

少し前に起きたエラーで書こうか迷ったのですがまた同じエラーでつまずきたくないので書くことにしました。少しでも参考になれば幸いです。

エラー内容

今回のエラーは僕の場合RSpecのテストのためにrails db:migrate RAILS_ENV=testを実行したところMysql2::Error: Unknown table 'myapp_test.notifications': DROP TABLE 'notifications'と出ました。

確認したこと・試したこと

mysqlでmyapp_testのテーブルを確認したがnotificationsは入っていなかった。myapp_developmentのテーブルにはちゃんとnotificationsが入っていた。
一応rails db:create RAILS_ENV=testをやってみたがすでに作られてるよと言われた。

対処方法

エラーとなったマイグレーションファイルがあるところを参照する
僕の場合テスト環境だったので、

$ rails db:migrate:status RAILS_ENV=test

と打つと,

database: myapp_test

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20201015014442  Create users
   up     20201015015259  Add index to users email
   up     20201015015729  Add password digest to users
   up     20201018020551  Add remember digest to users
   up     20201022031928  Add admin to users
   up     20201023063329  Create posts
   up     20201102044141  Add picture to posts
   up     20201103142232  Create relationships
   up     20201105025016  Create likes
   up     20201106231109  Create comments
   up     20201109071255  Create talks
   up     20201109072007  Create memberships
   up     20201109072128  Create messages
  down    20201127182835  Drop table notifications
  down    20201127183353  Create notifications

と出てきて、これを見るとCreate notificationsdownしているのが分かります。これをupにすればいいわけです!

$ rails db:migrate:up VERSION=20201127183353 RAILS_ENV=test

※もしupからdownにしたい場合は、

$ rails db:migrate:down VERSION=20201127183353 RAILS_ENV=test

これでrails db:migrate RAILS_ENV=testが無事通ることができました!

最後に

なぜdownになっていたのかわからないですがエラーを直すことができてよかったです。

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