Fixing django.db.utils.OperationalError: (1146, "Table 'dbname.table_name' doesn't exist")

beginner🐍 Python2026-07-26| Django 2.2+, Python 3.x, MySQL, MariaDB, PostgreSQL, or SQLite.

Error Message

django.db.utils.OperationalError: (1146, "Table 'dbname.table_name' doesn't exist")
#python#django#mysql#database-error#migrations

The 30-Second Fix

Your database is essentially saying, "I see what you're asking for, but that table isn't in my records." Most often, you simply forgot to push your model changes into the actual database schema. Run these two commands to sync them up:

python manage.py makemigrations
python manage.py migrate

If you're working on a specific feature, target that app directly to avoid noise:

python manage.py makemigrations your_app_name
python manage.py migrate your_app_name

Why is this happening?

Error 1146 is specific to MySQL and MariaDB. It triggers when Django’s Object-Relational Mapper (ORM) tries to query a table that doesn't exist in your current database. This isn't just a "missing table" problem; it's a desynchronization between your Python code and the SQL schema.

Common culprits include:

  • Ghost Migrations: You created a new Post model with 10 fields, but the database hasn't received the CREATE TABLE command yet.
  • The Registry Gap: You forgot to add a new app to your INSTALLED_APPS list. If Django doesn't track the app, it won't look for its migrations.
  • Eager Code: You wrote a query that runs the moment Python imports a file (e.g., in forms.py), before the database is even ready.
  • Manual Tampering: Someone (or another script) dropped a table directly in the MySQL console, leaving Django's django_migrations table confused.

Step-by-Step Solutions

1. Check Your App Registration

Django ignores migrations for any app not explicitly registered. Open settings.py and verify your app is present. It’s a simple check that saves hours of debugging.

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    # ...
    'blog_app',  # Ensure your app name is exactly right here
]

2. Solve the "Chicken and Egg" Query Problem

A frequent mistake involves querying the database at the top level of a file. For example, if you define choices for a dropdown by fetching all categories directly in forms.py, Django tries to run that SQL the moment you start the server.

# Don't do this! It runs on import.
CATEGORIES = [(c.id, c.name) for c in Category.objects.all()]

class ProductForm(forms.Form):
    category = forms.ChoiceField(choices=CATEGORIES)

If the category table doesn't exist yet (like during your first migration), the whole process crashes. The fix: Use ModelChoiceField, which stays "lazy" and only queries the database when the form is actually used.

class ProductForm(forms.Form):
    # This is safe and much cleaner
    category = forms.ModelChoiceField(queryset=Category.objects.all())

3. Recover from Desynced Migrations

Sometimes your database thinks a migration was applied, but the table is missing. Or perhaps the table exists, but Django tries to create it again. This is where the --fake flag becomes your best friend.

If the table exists in the database but Django keeps trying to create it, tell Django to record the migration as "done" without running the SQL:

python manage.py migrate your_app_name --fake

If you need to start fresh for a specific app during development, you can wipe the migration history for just that app:

  • Clear the migration history: python manage.py migrate --fake your_app_name zero
  • Remove the migration files (except __init__.py) in your app's migrations/ folder.
  • Regenerate and apply: python manage.py makemigrations then python manage.py migrate.

4. Direct Database Verification

If you're still seeing the error, look under the hood. Use the Django shell to see if the ORM can talk to the table at all. It takes five seconds and rules out configuration issues.

python manage.py shell

# Try a simple count query
from your_app_name.models import YourModel
print(YourModel.objects.count())

If this returns a number (even 0), your table is alive and well. If it throws the 1146 error again, hop into your SQL terminal and run SHOW TABLES; to see exactly what the database is holding.

Pro Tip: Multi-DB Environments

Working with a primary and a secondary database? Django might be looking at the wrong one. If your table lives in a secondary DB, you must specify it in your migration command:

python manage.py migrate --database=external_db

Further Reading

Related Error Notes