Skip to content
Latchkey

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

  1. Run db:create to make the test database.
  2. Run db:schema:load to create all tables from db/schema.rb.
  3. Run the tests once the tables exist.
.github/workflows/ci.yml
- run: |
    bin/rails db:create
    bin/rails db:schema:load
  env:
    RAILS_ENV: test

Use 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:prepare

How 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →