15

Given a newly created django project with the following installed apps:

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

When I run ./manage.py migrate for the first time I get the following error:

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages, registration
  Apply all migrations: sessions, admin, auth, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Creating table registration_registrationprofile
    Running deferred SQL...
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
    cursor.execute(statement)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist```

It seems Django is trying to create the registration tables before the user table.

This erros does not happen if I comment the registration app and run migrate and then uncomment the registration app and run migrate again. However, that's not the right way of doing it, right?

goathi
  • 353
  • 1
  • 4
  • 9
  • Django migrations should be able to specify dependencies to determine order, so the `registration` migration could probably use a `dependency` to `auth.User`. Since it's a library, I'm not really sure what the solution is here. Perhaps the order of `INSTALLED_APPS`? – Yuji 'Tomita' Tomita May 01 '15 at 23:33
  • I've already changed the order of INSTALLED APPS but it didn't work! :( Thanks by the way. – goathi May 03 '15 at 13:48
  • To "solve" it I had to create all migrations for all apps and then I launched a migrate and it worked. http://stackoverflow.com/questions/29689365/auth-user-error-with-django-1-8-and-syncdb-migrate (Pedro's asnwer) – Daviddd May 06 '15 at 17:08
  • I am having same issue. `python manage.py migrate` migrates all apps in correct order with auth first on mysql5.6 but when I move to mysql5.7, I see this issue. – Hussain Sep 06 '18 at 07:52

8 Answers8

24

After updating my Django version, I got this error and fix as running these two lines:

python manage.py migrate auth
python manage.py migrate

auth_user table inside auth model should run first I guess.

Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18
  • And if it looks like manage.py is packed away somewhere, it's okay, you can still use it- # python /usr/lib/python2.7/dist-packages/graphite/manage.py migrate auth – danno Aug 08 '17 at 18:05
  • 1
    At long last, the answer I needed. A gazillion answers saying to run `manage makemigrations` and `manage migrate` but this is the first I've seen that mentions `manage migrate auth`. Is this due to originating my app with Django 1.6 and upgrading along the way to 2.0 now? Or are most of these answers just incomplete? – JFlo Oct 30 '18 at 15:25
  • More people need to see this answer. It solved my issue after a lot of searching. Thank you. – Ryan Feb 11 '19 at 21:33
4

The problem is avoided when you do as Pedro Wagner suggests (auth_user error with Django 1.8 and syncdb / migrate):

Make sure that for all your apps, there exist initial migrations files by running:

manage.py makemigrations my_app

I'd do it not only for those that depend on auth because I think the problem is more general.

The root cause for this behaviour seems to me that for some reason

manage.py makemigrations

does not always create the initial migrations if they are not already there, contrary to:

manage.py makemigrations my_app

Unfortunately I cannot fathom the reasons for this asymmetry.

Community
  • 1
  • 1
Ytsen de Boer
  • 2,797
  • 2
  • 25
  • 36
4

I think you just forgot to migrate your auth models. However, to do that, just type in the following command on your terminal.

python manage.py migrate 

or

python manage.py migrate auth

Hope this settles your error in the program.

nishit chittora
  • 974
  • 13
  • 20
1

I think you needed to run:

python manage.py syncdb

and potentially you need to setup some dependencies? Probably not necessary after syncdb.

South 1 style (Django < 1.7)

class Migration:

    depends_on = (
        ("accounts", "0001"),
    )

    def forwards(self):
        ....

South 2 style (Django >= 1.7)

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [("accounts", "0001")]
Eric Carmichael
  • 560
  • 4
  • 15
0

Once you create a migration for your app it will automatically add the necessary dependencies. So in this case just run ./manage.py makemigrations registration.

Please check the registration/migrations/0001_initial.py file and you should see something like this:

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

This means you need to create migrations for all your apps with any kind of dependency.

Hassek
  • 8,715
  • 6
  • 47
  • 59
0

I had this problem too, solved it by replacing old registration with one that includes pull #25:

pip install git+https://github.com/macropin/django-registration.git@v1.2c0
Vincent van Leeuwen
  • 681
  • 1
  • 9
  • 17
0

In my case, this has been resolved re-adding some modules in INSTALLED_APPS that had been removed. As a consequence, some tables in the database were confusing the migration scheme and then ruining the test command because the default database was containing those previous migrations.

Fixed re-adding the modules, allauth and other related submodules in my case.

yomguy
  • 66
  • 3
-2

you have not migrated your models

python manage.py makemigrations my_app_name

for mac os

python3 manage.py makemigrations my_app

pujan rai
  • 9
  • 2