[RUBY] RSpec gives ActiveRecord :: StatementInvalid: Could not find table

Error

ActiveRecord::StatementInvalid: Could not find table

Can't find DB table when running test

What I tried

rails db:migrate:reset RAILS_ENV=test

Reset the test DB with

rails dbconsole -e test

sqlite> .table

Check if the table is made with. Created.

Many people think this is the solution, but if you run the test again,

ActiveRecord::StatementInvalid: Could not find table

Same error.

And when I checked the table again, the table disappeared.

What I tried based on this

Remove ActiveRecord :: Migration.maintain_test_schema!

in rails_helper.rb

rails_helper.rb


#Code to comment out
ActiveRecord::Migration.maintain_test_schema!

I commented this out.

Then I passed. However, the table has not been created yet, and all the tests using the table have failed.

What is ActiveRecord :: Migration.maintain_test_schema!

Code for automatically migrating test DB by looking at schema.rb before executing rspec.

I thought that there may be a problem with the schema that the test is executed by removing this, so I confirmed it.

Could not dump table "table name" because of following StandardError

It was written like this in the schema file.

The table cannot be reflected in the schema.

Check the migration file

Then

migration_name.rb


  def up
    change_column :comments, :discovery_id, :reference, null: true
  end

There was a part that was supposed to be.

There is no reference in the type, but for some reason I did this.

Change: reference to: integer, rails db: migrate: reset Was described in the schema.

Run again

Reset migration again after being well documented in schema

rails db:migrate:reset RAILS_ENV=test

And uncomment ActiveRecord :: Migration.maintain_test_schema!

rails_helper.rb


#Uncomment
ActiveRecord::Migration.maintain_test_schema!

And run the test

bin/rspec

I passed all the tests.

Summary

Test execution → Table not found → Reset and migrate in test environment → Table was created → Test execution again → Missed and not found → Do not read the schema just before execution → Test works → Table not found in schema is described Not done → Migrate properly → Test run

It was like this.

Recommended Posts

RSpec gives ActiveRecord :: StatementInvalid: Could not find table
gradle Could not find tools.jar. Error countermeasures
ActiveRecord :: StatementInvalid (SQLite3 :: SQLException: no such table: main.follows ~