14

I am running into a ProgrammingError when doing migrate, I think it may be related to the use of django-allauth with a custom user. Here is what I do

1/ Create a fresh database with psql:

create database dj_example;

2/ Installed_apps contain django.contrib.sites:

DJANGO_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
)
THIRD_PARTY_APPS = (
'crispy_forms',  # Form layouts
'allauth',  # registration
'allauth.account',  # registration
#'allauth.socialaccount',  # registration
#'allauth.socialaccount.providers.twitter',
'djcelery', #Celery
)
LOCAL_APPS = (
'tucat.users',  # custom users app
}

3/ site_id is set to 1

SITE_ID = 1

4/ Custom user model is overly simple:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
  def __unicode__(self):
    return self.username

5/ Makemigrations works fine

# python manage.py makemigrations
Migrations for 'djcelery':
  0028_auto_20160601_1919.py:
  - Alter field status on taskmeta

6/ Migrate returns ProgrammingError: relation "users_user" does not exist

# python manage.py migrate
  Operations to perform:
  Apply all migrations: auth, contenttypes, djcelery, account, admin, sessions
  Running migrations:
  Rendering model states... DONE
  Applying account.0001_initial...Traceback (most recent call last):
  File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "users_user" does not exist

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 12, in <module>
  execute_from_command_line(sys.argv)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
  utility.execute()
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
  self.execute(*args, **cmd_options)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
  output = self.handle(*args, **options)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
  executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
  self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
  state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
  state = migration.apply(state, schema_editor)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 90, in __exit__
  self.execute(sql)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
  cursor.execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
  return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
  raise value.with_traceback(tb)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_user" does not exist

Any idea of how to resolve that problem?

Antoine Brunel
  • 1,065
  • 2
  • 14
  • 30
  • 1
    Have you run initial migrations for `tucat.users` yet? You probably have to do that before any others. Also, changing the default user model might be very tricky in an existing project. You might find it easier to trash your existing migrations and create new ones. – Håken Lid Jun 01 '16 at 17:39
  • I just tried # python manage.py makemigrations users, then # python manage.py migrate users, but now it returns another exception: psycopg2.ProgrammingError: relation "django_site" does not exist LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1 ... django.db.utils.ProgrammingError: relation "django_site" does not exist LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1 – Antoine Brunel Jun 01 '16 at 18:01
  • 1
    Yes. I should have written "before any custom or third party apps". It looks like you have to do the django core apps first (`django.contrib.*`) – Håken Lid Jun 01 '16 at 18:04
  • It worked, thank you so much! If you want, you can post the answer so that I can declare it as resolved :-) – Antoine Brunel Jun 01 '16 at 18:35

3 Answers3

25

You have to delete the migrations folder and then, you should do

python manage.py migrate --run-syncdb

python manage.py migrate --fake appname
Shinto Joseph
  • 2,809
  • 27
  • 25
  • 1
    Half a day trying to and these two lines did the trick. Thanks @Shinto Joseph ! – VivienG Jul 31 '18 at 18:50
  • @Shinto Joseph I had this problem too, it took me two hours to find this. What causes this? Wrong order of migrations when first installing a project/app? – Doug Smith Sep 03 '18 at 04:10
  • Deleting the migrations folder cause errors when I tried to migrate, per this answer https://stackoverflow.com/a/30438978/6054814 the migrations folder needs to exist and contain `__init__.py` for makemigrations and migrate to work correctly. Doing that step caused my errors to disappear. – anOkCoder Sep 17 '18 at 04:04
  • I only needed the first line to get it working. Thanks so much! – ekmcd Aug 28 '20 at 21:22
8

Your error is caused by the order you run the migrations. Since many apps depend on the user model existing, you must run the initial migrations for your custom user app before those other apps.

If you change the default user model in an existing project, it might be easier to discard all existing migrations (and the database) and rebuild from scratch. The order to apply migrations would be:

  1. The core django.contrib apps.
  2. Your custom user app.
  3. Other custom apps and third party apps.

You can use django-admin showmigrations to see which migrations exists and are planned.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • What if discarding the database is not an option? – Laith Tahboub Jun 29 '22 at 10:00
  • 1
    @LaithTahboub It is still doable. But it is more complex. You might have to hand write SQL instead of using Django's migrations, for example. I can't give a more specific answer, because the details depend very much on how you have structured your database / Django models. – Håken Lid Jun 30 '22 at 23:38
0

FYI-

I had this problem, and to resolve it I had to comment out all references to views.py in my urls.py and url_tenants.py files. Then, I ran makemigrations and got the database tables to create, then run migrate_schemas, and later uncomment the url files. Hope this helps someone.

Mattchoo
  • 401
  • 4
  • 7