ActiveRecord::StatementInvalid: Could not find table
Can't find DB table when running test
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.
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.
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.
It was written like this in the schema file.
The table cannot be reflected in the schema.
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.
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.
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.