Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I had a working project with django 1.7, and now I moved it to django 1.8. I can do syncdb and run the app with sqlite, but when I switch to postgres, it fails to do syncdb:

  Creating tables...
    Creating table x
    Creating table y
    Running deferred SQL...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "~/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "~/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "~/venv/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "~/venv/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "~/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "~/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "~/venv/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "~/venv/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 "~/venv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
    cursor.execute(statement)
  File "~/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "~/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "~/venv/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "~/venv/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

I tried deleting the database and recreating it. Also, I tried:

python manage.py migrate auth

which also fails:

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

LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1

Please help get this fixed.

share|improve this question
    
And then if you switch back to sqlite for your database backend and start from an empty database, it works again? – Louis Jun 17 '15 at 16:51
1  
This may have something to do with changes in Django 1.8: try using the '--fake-initial' option when you migrate if the tables already exist; it used to be implicit, but has now been made explicit: docs.djangoproject.com/en/1.8/ref/django-admin/… Did you try deleting the SQLite DB or the PostgreSQL DB? – FlipperPA Jun 17 '15 at 17:02
    
in 1.8, there is makemigrations and migration – itzmeontv Jun 17 '15 at 17:12
    
Yes. I've tried deleting the database. When I run python manage.py migrate --fake-initial myapp it gives me the error: the app does not have migrations (you cannot selectively sync unmigrated apps) – max Jun 17 '15 at 20:42
    
It seems to be working now. I deleted all the pyc files. – max Jun 17 '15 at 20:44

I didn't like the idea of commenting/uncommenting code, so I tried a different approach: I migrated "manually" some apps, and then run django-admin.py migrate for the remaining ones. After deleting all the *.pyc files, my sequence of commands was:

$ django-admin.py migrate auth
$ django-admin.py migrate contentypes
$ django-admin.py migrate sites
$ django-admin.py migrate MY_CUSTOM_USER_APP
$ django-admin.py migrate

where MY_CUSTOM_USER_APP is the name of the application containing the model I set AUTH_USER_MODEL to in my settings file.

Hope it can help. Btw, it seems strange that the best way to synchronize your db in Django 1.8 is so complicated. I wonder if I'm missing something (I'm not very familiar with Django 1.8, I used to work with older versions)

share|improve this answer

Always migrate db with python manage.py makemigrations and then python manage.py migrate in newer versions. For the error above if first time your are migrating your database then use python manage.py migrate --fake-initial. See docs https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-migrate

share|improve this answer

I had the same problem, and I spent hours banging my head trying to find a solution, which was hidden in the comments. My problem was that CircleCI couldn't run tests because of this error. And I thought I would need to start fresh with a new and empty DB. But I got the same errors. Everything was seemingly related to 'auth', 'contenttypes' and 'sites'.

I read this, and this, as well as this and also this. None were solutions for me.

So after having destroyed my DB and created a new one, the only solution I found to entirely avoid these django.db.utils.ProgrammingError was to:

  1. Comment out all code related to the User model.
  2. Delete all .pyc files in my project! find . -name "*.pyc" -exec rm -- {} + Thanks @max!
  3. run ./manage.py migrate (no fake, no fake-initial, no migration of 'auth' or 'contenttypes' before, juste plain migrate.
  4. Uncomment the above code, and run migrate again!

My INSTALLED_APP is the following:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sites',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'mptt',
    'djangobower',
    'honeypot',
    'django_hosts',
    'leaflet',
    'multiselectfield',
    'corsheaders',
    'rest_framework_swagger',
    'allauth',
    'allauth.account',
    # 'allauth.socialaccount',
    # 'allauth.socialaccount.providers.twitter',
    # 'allauth.socialaccount.providers.facebook',
    'project.<app_name>',
)
share|improve this answer
1  
The second time you run migrate, the code you commented out will not run, sure, but what happens when you want to deploy your application elsewhere, where the migrations will have to run from scratch? If the user-related code is not necessary, you should just get rid of it. – borfast Feb 5 at 23:22

Working on Django 1.10 I found out another solution: My application is named "web", and first I call:

python manage.py makemigrations web

then I call:

python manage.py makemigrations auth

then I call:

python manage.py migrate

Amazed: IT'S WORKING! :) It seems auth was searching for the AUTH_USER_MODEL "web.UserProfile" and a relation named web_user_profile, and it didn't find it, hence the error. On the other hand, calling makemigrations web first creates the required relation first, before auth is able to check and alert it's not there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.