如何解决Django中django.db.utils.ProgrammingError: relation "some_object" does not exist错误?
Hey there, I know how frustrating it is when migrations go haywire—especially when you can’t just wipe and rebuild your production database. Let’s break down what’s likely going on here and walk through actionable fixes that don’t require starting from scratch.
Why This Happens
First, let’s diagnose the root causes:
- Migration History Mismatch: Your colleague might have used
--fakelocally, deleted old migration files, or merged migrations without updating the team. This leaves yourdjango_migrationsdatabase table out of sync with the actual migration files in your codebase. Django thinks a migration ran (and createdsome_object), but the table never actually got built. - Broken Migration Dependencies: A migration might be trying to reference
some_objectbefore the migration that creates it runs. This happens if thedependencieslist in a migration file is missing or ordered incorrectly.
Step-by-Step Fixes
1. Audit Your Migration History
First, map out what Django thinks happened vs. what actually exists:
- Run this to see which migrations are marked as applied locally:
python manage.py showmigrations - Connect to your PostgreSQL database and run this query to check the official migration history:
SELECT app, name, applied FROM django_migrations ORDER BY applied;
Compare the two lists. Look for:
- Migrations marked as applied in the DB but missing from your code.
- Migrations present in your code but not marked as applied in the DB (especially the one that creates
some_object).
2. Targeted Migration Adjustments
If you find the migration that’s supposed to create some_object exists in your code but isn’t in the django_migrations table:
- Run that specific migration directly:
python manage.py migrate your_app 00XX_create_some_object
If Django claims the migration is already applied (but the table doesn’t exist):
- Manually remove the incorrect entry from the migration history:
DELETE FROM django_migrations WHERE app = 'your_app' AND name = '00XX_create_some_object'; - Run the migration normally:
python manage.py migrate your_app
3. Fix Broken Dependencies
Open the migration that’s throwing the error (the one referencing some_object). Check its dependencies list—does it include the migration that creates some_object? For example:
# Bad: missing critical dependency dependencies = [ ('your_app', '0001_initial'), ] # Good: explicitly depends on the migration that creates some_object dependencies = [ ('your_app', '0002_create_some_object'), ]
After fixing the dependency, if you already tried to run the problematic migration, fake it first to reset Django’s history:
python manage.py migrate --fake your_app 00XX_problematic_migration
Then run migrations normally:
python manage.py migrate
4. Debug with sqlmigrate
If you’re still stuck, see exactly what SQL Django is trying to run for the problematic migration:
python manage.py sqlmigrate your_app 00XX_migration_name
This will show you the raw SQL. Check if some_object is being created here, or if the migration is trying to query/alter it before it exists. You can even run the SQL directly in PostgreSQL (carefully!) if Django’s migration system is being stubborn.
5. Prevent Future Issues
- Tell your team: never edit existing migration files—always create new ones with
makemigrations. - Avoid using
--fakeunless you fully understand what it does (it only updates the migration history, not the database). - Consider adding
django-migration-linterto your project—it catches dependency issues and bad migration practices before they get merged.
For Production
When deploying to production, follow the same steps:
- Back up your production database first (critical!).
- Audit the
django_migrationstable vs. your codebase. - Fix dependencies or adjust the migration history as needed using the targeted steps above.
- Run migrations normally—no need to wipe the database.
内容的提问来源于stack exchange,提问作者Kwall

