Rails "Could not load schema" / empty test schema in CI
The test database was created but its schema was never loaded, so the first query against a table fails. Rails needs db:schema:load (or db:migrate) after db:create in CI.
What this error means
Tests fail with "PG::UndefinedTable: ERROR: relation \"users\" does not exist" or "Mysql2::Error: Table ... doesn't exist" even though the database connects.
Rails
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist
LINE 1: SELECT "users".* FROM "users"Common causes
db:create ran but schema was never loaded
Creating the database only makes an empty database; without db:schema:load or db:migrate there are no tables.
schema.rb is out of date or not committed
If db/schema.rb is missing or stale, loading it produces the wrong tables and queries still fail.
How to fix it
Load the schema after creating the database
- Run
db:createto make the test database. - Run
db:schema:loadto create all tables fromdb/schema.rb. - Run the tests once the tables exist.
.github/workflows/ci.yml
- run: |
bin/rails db:create
bin/rails db:schema:load
env:
RAILS_ENV: testUse db:prepare as a one-liner
db:prepare creates, loads the schema, and migrates as needed in a single idempotent step.
Terminal
RAILS_ENV=test bin/rails db:prepareHow to prevent it
- Always pair db:create with db:schema:load (or use db:prepare).
- Commit an up-to-date db/schema.rb.
- Keep the schema format (:ruby or :sql) consistent between local and CI.
Related guides
Rails "ActiveRecord::NoDatabaseError" (missing test database) in CIFix Rails "ActiveRecord::NoDatabaseError" in CI - the test database does not exist yet because no step ran ra…
Rails "ActiveRecord::PendingMigrationError: Migrations are pending" in CIFix Rails "ActiveRecord::PendingMigrationError: Migrations are pending" in CI - the test database schema is b…
ActiveRecord::StatementInvalid in CIFix "ActiveRecord::StatementInvalid" in CI - a SQL statement failed (undefined column/table, type mismatch, o…