Search code examples
pythondjangodjango-rest-frameworkdjango-migrations

Django migration didn't migrate authtoken and sessions


When running python manage.py migrate not all migrations were run, specifically django_celery_results, authtoken and sessions. This resulted in the application related migrations erroring out.

The terminal output looks like so:

However, if I first manually migrate those three, and then specifically migrate auth (not sure why I'd need to migrate that again) and then do python manage.py migrate it'll work.

enter image description here

The installed apps on Django are like so:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'django_celery_results',
    'celery.contrib.testing.tasks',
    'api_app'
]

I'm wondering why that's happening, I thought migrate will run all the migrations listed in "operations to perform".


Solution

  • Your api_app.0002 migration creates a user without setting last_login. Therefore this migration must be run after the auth 0005 migration that allows nulls in this column.

    If you add a dependency to your migration, then Django will run them in the correct order.

    class Migration(migrations.Migration):
    
        dependencies = [('auth', '0005_alter_user_last_login_null')]