Rails 5, Rspec: Environment data not found in the schema

前端 未结 7 1126
南方客
南方客 2020-12-29 01:49

After upgrading a Rails app to Rails 5, running RSpec tests gives me the following error:

rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

Environm         


        
相关标签:
7条回答
  • 2020-12-29 02:35

    For me, I had to do a mixture of things:

    bin/rails db:environment:set RAILS_ENV=test
    bin/rails db:migrate RAILS_ENV=test
    

    This would make things work, and then I had to review my migrations, I was adding a null:false into a relationship and that added a bug, the migration was cancelled and didn't finish

    0 讨论(0)
  • 2020-12-29 02:35

    If you happen to see this error while using parallel_tests gem then you need to run below command for each DB. Just increase TEST_ENV_NUMBER.

    TEST_ENV_NUMBER=1 bin/rails db:environment:set RAILS_ENV=test
    TEST_ENV_NUMBER=2 bin/rails db:environment:set RAILS_ENV=test
    

    This helped me fix the problem when I was testing parallel_tests with knapsack_pro gem https://github.com/KnapsackPro/knapsack_pro-ruby#parallel_tests-with-knapsack_pro-on-single-ci-machine

    0 讨论(0)
  • 2020-12-29 02:37

    New Rails 5 command to generate binstubs:

    rails app:update:bin
    

    Allows me to run the solution as the error suggested:

    bin/rails db:environment:set RAILS_ENV=test
    

    Tip from @max comment: If you are using database_cleaner and this error keeps popping up then change your config to:

    DatabaseCleaner.clean_with(
      :truncation,
      except: %w(ar_internal_metadata)
    )
    
    0 讨论(0)
  • 2020-12-29 02:39

    For me, this error was followed by a similar one asking for a migration.

    This did the trick: rails db:migrate RAILS_ENV=test

    0 讨论(0)
  • 2020-12-29 02:44

    All of the above answers are correct, however, if you're in a more unique project such as developing a rails engine with a concept of a schema (hacky, I know) and your migration fails for some reason, you can re-run it without the check that throws this exception. Example:

    rake environment db:{drop,create,migrate} DISABLE_DATABASE_ENVIRONMENT_CHECK=1

    0 讨论(0)
  • 2020-12-29 02:48

    fix for jenkins before you drop database you should execute db:environment:set with || true, so the command doesn't abort:

    bin/rails db:environment:set RAILS_ENV=test || true
    
    0 讨论(0)
提交回复
热议问题