[RUBY] A story that says Table doesn't exist even though there is a table

Introduction

The other day, after turning off the power of the PC, I started docker again and entered docker-compose run wen rails s, but I got the following error.

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

Even now, the main cause has not been clarified. In this article, I will describe what I have tried to solve.

The main cause of the error was unknown, but the error has been resolved and development itself is in progress. If you are suffering from similar errors, please refer to it.

If anyone knows the cause, I would appreciate it if you could let me know in the comments.

Development environment

Ruby 2.72 Rails 6.0.2.3 MySQL 5.7 Docker/docker-compose

Error that occurred

I mentioned it briefly at the beginning, but I got the following error. It is said that there is no books table.

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'

#Omission

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'

#Omission

/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)

What I tried

① ** Check the migration execution status ** First, I checked the migration execution status.

$ 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

I found that all the migration files are running.

② ** Check database ** Then I connected to mysql and checked the database table.

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

Then, it was displayed as follows. Does the books table exist?

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

However, the following command says that there is no books table.

> select * from books;

You can get the data of other tables.

> 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 |
+----+------+---------------+---------+----------------------------+----------------------------+

When I looked up the official documentation, it only mentioned what I had tried.

If the error Table'xxx' doesn't exist or Can't find file:'xxx' (errno: 2) occurs, it indicates that there is no table in the current database named xxx. You can use SHOW TABLES to see which tables are in the current database.

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

③ ** Rebuilding the database ** I didn't feel like it, but I decided to recreate the database.

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

This solved the error! Since it was a personal development, I could just delete the database, but I don't think this method is recommended in the actual field. If anyone knows the cause of this error, I would appreciate it if you could let me know in the comments.

Recommended Posts

A story that says Table doesn't exist even though there is a table
A small story that is sometimes useful in Maven
Phenomenon that cannot log out even though it is described in devise
A story that suffered from a space that does not disappear even if trimmed with Java The cause is BOM