MySQL Error 1146: Table Doesn't Exist

MySQL error 1146 (Table Doesn't Exist) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

MySQL Error Code 1146 MySQL Last verified 2026-08-19

Quick Answer

Verify the schema context: `SELECT DATABASE();` — if NULL, run `USE dbname;` or prefix tables with dbname.. If that does not apply, list tables to confirm the exact name: `SHOW TABLES;` or `SHOW TABLES FROM dbname LIKE '%pattern%';` — the full checklist is below.

Error Code

MySQL Error Code: 1146
Official name: Table Doesn't Exist
Service: MySQL

What does this error mean?

Table '%s.%s' doesn't exist

Common Causes

How to Fix

  1. Verify the schema context: `SELECT DATABASE();` — if NULL, run `USE dbname;` or prefix tables with dbname.
  2. List tables to confirm the exact name: `SHOW TABLES;` or `SHOW TABLES FROM dbname LIKE '%pattern%';`
  3. Check case sensitivity: on Linux, `Users` and `users` are different tables if lower_case_table_names=0
  4. If a migration should have created it, run the pending migration (e.g. `prisma migrate deploy`, Rails db:migrate, Flyway)
  5. Confirm the config actually connects to the environment you think it does

Code Examples

Check the active schema sql
SELECT DATABASE();
SHOW TABLES;
SELECT * FROM dbname.users LIMIT 1;  -- fully qualified

Confirms which database is active and whether the table exists under its real name.

Framework-Specific Fixes

nodejs-mysql2

Log the failing table from err.sql and include the schema in the query to avoid cross-db mistakes.

pool.query('SELECT * FROM users WHERE id = ?', [id], (err, rows) => {
  if (err && err.code === 'ER_NO_SUCH_TABLE') console.error(err.sql)
})
python-sqlalchemy

If a test run creates tables, make sure create_all / migrations run before queries.

Base.metadata.create_all(engine)
# or run migrations: alembic upgrade head
php-pdo

Always qualify the database in the DSN so PDO never lands in the wrong schema.

$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass);
$pdo->exec('USE ' . $db);

You Might Also Like

Frequently Asked Questions

Why am I seeing MySQL error 1146?

Most often this happens when a typo in the table name, or wrong database selected (no `USE db` / no db prefix in the query), or when case mismatch: table names are case-sensitive on Linux depending on lower_case_table_names.

How do I fix MySQL error 1146?

Verify the schema context: `SELECT DATABASE();` — if NULL, run `USE dbname;` or prefix tables with dbname..

Which frameworks have documented fixes for error 1146?

This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.

Official Sources

This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.