Full width home advertisement

Post Page Advertisement [Top]

Fixing Django InconsistentMigrationHistory Error (Custom User Model)

The Problem

After resetting my database and running migrations, I encountered the following error. 

django.db.migrations.exceptions.InconsistentMigrationHistory:

Migration admin.0001_initial is applied before its dependency mail.0001_initial

Later, when trying to create a superuser, I also saw

django.db.utils.OperationalError: no such table: mail_user


Root Cause

The issue was caused by a mismatch between the database state and the migration files. The admin app had already been migrated, but the mail app(which contains the custom User model) had not. Since Django's authentication system depends on the user model, this caused a dependency conflict. 


Solution

The safest way to fix this was to completely reset the database and migrations. 

1. Remove the database

Remove-Item db.sqlite3 -Force

2. Clear migration files

Remove-Item mail\migrations\*.py

Then recreate the required file

New-Item mail\migrations\__init__.py -ItemType File (PowerShell)

type nul > mail\migrations\__init__.py (CMD)

3. Ensure custom user model is configured

In settings.py: AUTH_USER_MODEL = 'mail.User'

This must be set before running migrations. 

4. Create new migrations

python manage.py makemigrations mail

5. Apply migrations

python manage.py migrate


* Always define AUTH_USER_MODEL before running migrations

* Migration order matters when dependencies exist

* If migrations become inconsistent, a full reset is often the fastest fix

* PowerShell commands differ from CMD


This issue helped me better understand how Django handles: Migration dependencies, Custom user models, Database state consistency. If you're working with a custom user model, it's important to get the migration setup right from the beginning. 





댓글 없음:

댓글 쓰기

Bottom Ad [Post Page]