ERROR database schema_error ai_generated true

psycopg2.errors.UndefinedTable: ERROR: relation "my_table" does not exist

ID: database/relation-does-not-exist

Also available as: JSON · Markdown
91%Fix Rate
93%Confidence
65Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
16 active

Root Cause

The referenced table or view does not exist in the current database or search_path schema. Common causes include missing migrations, wrong database, wrong schema, or case-sensitive table name quoting.

generic

Workarounds

  1. 90% success Run pending database migrations to create the missing table
    Run the application's migration tool (e.g., 'alembic upgrade head', 'django manage.py migrate', 'knex migrate:latest'). Verify with '\dt' in psql to confirm the table exists.
  2. 85% success Check the search_path and connect to the correct schema
    Run 'SHOW search_path;' in psql. If the table is in a non-public schema, either set search_path: 'SET search_path TO myschema,public;' or fully qualify the table name: 'SELECT * FROM myschema.my_table;'.

Dead Ends

Common approaches that don't work:

  1. Wrapping the table name in double quotes without checking if the table actually exists 75% fail

    Double-quoting makes PostgreSQL treat the name as case-sensitive, which may fix a case mismatch but does nothing if the table was never created. This leads to debugging the wrong problem.

  2. Dropping and recreating the database without running migrations 80% fail

    Dropping the database removes all data and without running migrations afterward, the tables still will not exist. This is destructive and does not solve the underlying issue.

Error Chain

Leads to:
Preceded by:
Frequently confused with: