pythondjangodjango-authenticationdjango-syncdbdjango-fixtures

Django superuser fixture error - no such table: auth_user


I want to define a fixed username and password for superuser creation in Django's syncdb (after it has been executed). The method I'm using below was working in an older version of Django (I guess 1.6) but now it's not working.

I have this fixture file initial_data.json :

[
{
  "fields": {
    "username": "me",
    "first_name": "",
    "last_name": "",
    "is_active": true,
    "is_superuser": true,
    "is_staff": true,
    "last_login": null,
    "groups": [],
    "user_permissions": [],
    "password": "pbkdf2_sha256$...",
    "email": "a@a.co",
    "date_joined": "2015-04-23T01:13:43.233Z"
  },
  "model": "auth.user",
  "pk": 1
}
]

when I add this in settings.INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_app',

)

and run python manage.py syncdb, I get the following error :

django.db.utils.OperationalError: Problem installing fixture 'path/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user

What should I do?

Can I change the order that fixtures load to ensure that auth_user table is created before this fixture is loaded?

Or any other way to automatically create a superuser in Django?

Thanks in advance.


Solution

  • I solved my problem, however it is definitely not the prettiest solution.

    I saw that when I run heroku run python manage.py syncdb, I get the following

    Operations to perform:
      Synchronize unmigrated apps: myproject, permissions, myapp1, myapp2
      Apply all migrations: auth, flatpages, admin, contenttypes, sessions, sites
    Synchronizing apps without migrations:
      Creating tables...
        Creating table app1_model1
        Creating table app1_model2
        ...
    

    So I thought what if i migrate the included apps in reverse order :

    heroku run python manage.py migrate sites; heroku run python manage.py migrate sessions; heroku run python manage.py migrate contenttypes; heroku run python manage.py migrate admin; heroku run python manage.py migrate flatpages; heroku run python manage.py migrate auth;
    

    and it worked! I have no idea why, but it did. Maybe this will help someone else too.