LoginSignup
2
1

More than 3 years have passed since last update.

テーブルが存在するのに、Table doesn't existと言われた話

Last updated at Posted at 2021-01-16

はじめに

先日、PCの電源を閉じた後、再度dockerを起動し、docker-compose run wen rails sと入力すると、以下のようなエラーが出てしまいました。

Mysql2::Error: Table 'share_read_development.books' doesn't exist

現在でも、主原因は解明できておりません。
本記事では、僕が解決に向けて試したことを記載していきます。

エラーが起きた主原因は不明でしたが、エラーの解決は済んでおり開発自体は進んでおります。
もし同じようなエラーに苦しんでいる方は、参考にしてみてください。

また原因がわかる方がいらっしゃいましたら、コメントで教えていただけると幸いです。

開発環境

Ruby 2.72
Rails 6.0.2.3
MySQL 5.7
Docker/docker-compose

発生したエラー

冒頭でも簡単に記載しましたが、以下のようなエラーが出ました。
booksテーブルがないと言われています。

rails aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'share_read_development.books' doesn't exist
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:201:in `block (2 levels) in execute'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'

# 中略

Caused by:
Mysql2::Error: Table 'share_read_development.books' doesn't exist
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query'

# 中略

/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:schema:dump
(See full trace by running task with --trace)

試したこと

マイグレーションの実行状況の確認
まず、マイグレーションの実行状況を確認しました。

$ docker-compose run web rails db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20201207135139  Create users
   up     20201213135207  Create reviews
   up     20201218052640  Create books
   up     20201218075413  Add bookgenreid to books
   up     20201220063200  Add book to reviews
   up     20201222080803  Create relationships
   up     20201222131717  Create favorites
   up     20201222233733  Create comments
   up     20201224140712  Create bookcases
   up     20201225105949  Create notifications
   up     20201226102230  Add status to review
   up     20201230042031  Add caption to books

全てのマイグレーションファイルが実行されていることがわかりました。

データベースの確認
次にmysqlに接続し、デーベースのテーブルを確認しました。

$ docker-compose run web rails db
> show tables;

そうすると、以下のように表示されました。
booksテーブルは存在している?

+----------------------------------+
| Tables_in_share_read_development |
+----------------------------------+
| ar_internal_metadata             |
| bookcases                        |
| books                            |
| comments                         |
| favorites                        |
| notifications                    |
| relationships                    |
| reviews                          |
| schema_migrations                |
| users                            |
+----------------------------------+

しかし、以下のコマンドでは、booksテーブルがないと言われてしまいます。

> select * from books;

他のテーブルのデータは取得できます。

> select * from bookcases;
+----+------+---------------+---------+----------------------------+----------------------------+
| id | read | book_id       | user_id | created_at                 | updated_at                 |
+----+------+---------------+---------+----------------------------+----------------------------+
|  1 |    0 | 9784296108008 |       1 | 2020-12-28 01:18:40.540283 | 2020-12-28 01:18:40.540283 |
|  3 |    0 | 9784478820094 |       3 | 2020-12-28 12:39:00.697316 | 2020-12-28 12:39:00.697316 |
| 37 |    0 | 9784478820094 |       1 | 2020-12-29 08:16:25.944979 | 2020-12-29 08:16:25.944979 |
| 39 |    0 | 9784804614151 |       1 | 2020-12-29 08:26:56.615557 | 2020-12-29 08:26:56.615557 |
| 41 |    0 | 9784907095536 |       1 | 2020-12-29 08:27:01.301378 | 2020-12-29 08:27:01.301378 |
| 42 |    0 | 9784908925658 |       1 | 2020-12-29 08:39:11.191829 | 2020-12-29 08:39:11.191829 |
| 45 |    0 | 9784284204705 |       1 | 2020-12-30 04:10:08.090943 | 2020-12-30 04:10:08.090943 |
+----+------+---------------+---------+----------------------------+----------------------------+

公式ドキュメントを調べても、自身で試したことしか記載していませんでした。

エラー Table 'xxx' doesn't exist または Can't find file: 'xxx' (errno: 2) が発生する場合、xxx という名前のカレントデータベースにテーブルがないことを示しています。
SHOW TABLES を使用して、カレントデータベースにあるテーブルを確認できます。

【URL】
http://download.nust.na/pub6/mysql/doc/refman/4.1/ja/cannot-find-table.html

データベースの作り直し
あまり気が進みませんでしたが、データベースを作り直すことにしました。

$ docker-compose run web rails db:reset
$ docker-compose run web rails db:create
$ docker-compose run web rails db:migrate

これでエラーが解消することができました!
個人開発なので、データベースを削除するという方法で済みましたが、実際の現場ではこの方法は推奨されないと思います。
もし、今回のエラーの原因等がわかる方がいらっしゃいましたらコメントにて教えていただけると幸いです。

2
1
1

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