Why do I get a "auth_user does not exist"error when running "migrate" on a newly created django project with "registration redux" app installed?
Asked Answered
U

8

17

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?

Unlearn answered 1/5, 2015 at 20:25 Comment(4)
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?Magen
I've already changed the order of INSTALLED APPS but it didn't work! :( Thanks by the way.Unlearn
To "solve" it I had to create all migrations for all apps and then I launched a migrate and it worked. #29689865 (Pedro's asnwer)Instate
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.Suppletion
T
27

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.

Trumpeter answered 25/5, 2015 at 23:8 Comment(3)
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 authHarrow
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?Fowling
More people need to see this answer. It solved my issue after a lot of searching. Thank you.Subrogation
G
5

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.

Georginegeorglana answered 14/1, 2016 at 12:27 Comment(1)
This was super HelpfulMigrate
I
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.

Isahella answered 19/3, 2021 at 2:43 Comment(0)
H
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")]
Hagar answered 15/5, 2015 at 9:14 Comment(0)
S
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.

Stockdale answered 16/7, 2015 at 16:45 Comment(0)
H
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/[email protected]
Hosanna answered 23/9, 2015 at 12:31 Comment(0)
A
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.

Aggrandize answered 26/5, 2023 at 7:39 Comment(0)
E
-2

you have not migrated your models

python manage.py makemigrations my_app_name

for mac os

python3 manage.py makemigrations my_app

Emelineemelita answered 12/9, 2020 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.