5

I recently upgraded Django to 1.8 and set up a new development database for a fresh start. Migrations and dependencies went well, safe the usual errors you get and you end up solving. The app is working locally now just fine.

However, I am getting an error when trying to run tests:

python manage.py test

This is the error I am getting:

django.db.utils.ProgrammingError: relation "auth_user" does not exist

Needless to say, Django's auth module is indeed installed and migrated in the app, so I am not sure what is going on.

Here is the full stacktrace in case you need to peek at it, but it does't say anything even remotely helpful to me to figure out the cause of this error:

Traceback (most recent call last):
  File "C:/Users/dabadaba/PycharmProjects/dogpatchsports_com/mysite/manage_sched_dev.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 210, in run_tests
    old_config = self.setup_databases()
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 166, in setup_databases
    **kwargs
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 370, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\base\creation.py", line 368, in create_test_db
    test_flush=not keepdb,
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 318, in sync_apps
    cursor.execute(statement)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\dabadaba\Envs\django18\lib\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

I can figure out a workaround following this answer which prevents the test task from running migrate, which according to what I have been reading should be the point where the error happens. Odd enough, since when I run migrate everything is fine.

However, I would prefer not to resort to a cheeky workaround and stick to doing things as they are designed. Additionally, this error might be a hint that something else is actually wrong and ought to be fixed.

Some solutions suggest running:

python manage.py migrate auth
python manage.py migrate

But that does nothing since there are no pending migrations in my project.

How can I solve this mysterious issue?

dabadaba
  • 9,064
  • 21
  • 85
  • 155

2 Answers2

2

If you have any apps which have foreign keys to auth.User, then make sure that the initial migrations for those apps have a dependency on the auth app:

class Migration(migrations.Migration):

    dependencies = [('auth', '__first__')]
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Well they have this dependency apparently `migrations.swappable_dependency(settings.AUTH_USER_MODEL)` – dabadaba Oct 13 '17 at 09:38
  • Do you have a custom user model? – Alasdair Oct 13 '17 at 09:46
  • afaik we are using Django's native users. But here is the thing I just found out, do you think it's weird?: this is an attribute in one of our models: `admins = models.ManyToManyField('auth.User')`. Then from the shell I am getting an instance of that model from the db, and I am printing the type of `instance.admins.first()`, which outputs what I expected `django.contrib.auth.models.User`. Then how come the attribute is defined as `auth.User`? Maybe this is an error carried over by our Django upgrade? But how did migrations even work in the first place? Do you see anything weird here? – dabadaba Oct 13 '17 at 10:01
  • `'.'` is ok - see the example `'production.Manufacturer'` in the docs. The recommendation for the user model is to use `settings.AUTH_USER_MODEL` to make it more reusable, but that's not the problem here, `'auth.User'` is fine. – Alasdair Oct 13 '17 at 11:02
  • so basically `auth.User` is the same than `django.contrib.auth.models.User`? then why is my issue happening? – dabadaba Oct 13 '17 at 11:07
  • 1
    alright so I did this, for each app using `auth.User` I ran `python manage.py makemigrations appname` which created a migration file with the `auth.User` dependency as `migrations.swappable_dependency(settings.AUTH_USER_MODEL)`. However when running `python manage.py migrate appname` I am getting an error that says the relation already exists, i.e. `django.db.utils.ProgrammingError: relation "avatar_avatar" already exists` – dabadaba Oct 13 '17 at 11:40
  • That's sounds like a separate issue. If you have created a migration for a table that already exists, then you may have to use `--fake` to mark the migration as applied. Be very careful when using `--fake` - you can cause your migrations table to get out of sync with your database, which can be tricky to fix. – Alasdair Oct 13 '17 at 12:01
  • Yes, I used `--fake` and solved it. Thanks for everything! – dabadaba Oct 13 '17 at 12:03
1

I've had the same issue and finally it solved when I've migrated one of the apps that was included in the project but which migrations does't performed automatically.

So just check your apps and then perform by hand ./manage.py makemigrations YOUR_APP_NAME and ./manage.py migrate.

K. Stopa
  • 597
  • 8
  • 9