I am creating an application for a personal blog and after doing the first migrations and creating the superuser, without any application yet, it occurred to me to modify the user model, so I followed the answer to this question . This is my model:
# coding: utf-8
import uuid
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
bio = models.TextField(max_length=500, blank=True)
def __str__(self):
return self.username
Now, I proceed to prepare the tables, creating the initial migration:
manage.py@nspaces > makemigrations users
Migrations for 'users':
apps/users/migrations/0001_initial.py
- Create model User
Following files were affected
/Volumes/datos/Proyectos/nspaces/src/apps/users/migrations/0001_initial.py
But when applying the migration, I get an error:
manage.py@nspaces > migrate
...
# Muchas líneas para trazabilidad que no importan.
...
File "/Volumes/datos/datos_externos/entornos/datos_externos/nspaces/lib/python3.6/site-packages/django/db/migrations/loader.py", line 298, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
The error says that the migration of admin
which is a Django core application is applied before its dependency which is users
.
The short answer is: you can't .
The long answer is: It's so complicated, it's not worth the effort .
The model
User
in Django is related to the authentication and authorization of users, therefore it involves foreign relations and many-to-many relations with Django core tables and cannot be done automatically as they require them to be modified. manually the schemas, moving data from previous tables and surely some migrations need to be reapplied. There's an open case in project source control, if you're interested.Alternative
If the project is starting, as in my case, it is best to delete the database and the migrations folder and start over. Due to Django's limitations regarding changing model features, the model you reference
AUTH_USER_MODEL
must be created in the first migration of that app (usually called001_inital
), otherwise we'll see the error that is presented in the question.There are other possible bugs, which are documented on the Django site.