[Django]-Django 1.8 fails to django.db.utils.ProgrammingError: relation "auth_user" does not exist

16👍

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)

👤FSp

11👍

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.

8👍

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

6👍

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>',
)

2👍

Deleting migration files, associated .pyc files, and just to be safe all .pyc files with the following commands did not solve my issue.

$ find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
$ find . -path "*/migrations/*.pyc"  -delete
$ find . -name "*.pyc" -exec rm -- {} +

What ended up solving my issue wasn’t clearing caches, it was because I had a function that performed a query as a default function parameter. On init, which is what commands like makemigrations and migrate do before executing it seems like django (perhaps a python attribute?) initializes all default parameters.

As my database was completely empty (I needed to perform migrate --run-syncdb to recreate the tables) when the below default parameter was initialized, it ran a query against the empty database that subsequently failed.

change this:

def generate_new_addresses(number=1, index=None, wallet=get_active_wallet()):
   ...
   ...
   return

to:

def generate_new_addresses(number=1, index=None, wallet=None):
   if not wallet:
      wallet = get_active_wallet()
   ...
   ...
   return

2👍

I had the same issue, but my underlying cause was the __init__.py file in one of the migrations folders had been deleted from source code but not locally (causing ‘Not on my machine’ errors).

Migrations folders still need __init__.py files, even with Python 3.

1👍

In my case, this error was appearing when the postgresql driver was able to connect to the database, but the provided user does not have access to the schema or the tables, etc. Instead of saying permission denied, the error being shown is saying that the database table being queried is not being found. Typically in such a situation, the migrate command will also fail with a similar error when it tries to create the django_migrations table.

Check for access being granted on the user you’re using in database connection in Django.

0👍

I had this issues with a forms.ChoiceForm queryset. I was able to switch to using forms.ModelChoiceForm which are lazily evaluated and this fixed the problem for me.

👤adam b

0👍

In My Case
I made migrations using some other similar looking migration file(1).
Then I deleted it, and made 2 new migration files for replacement(2&3).
And then I was getting this error.

In my case the table was renamed using migration file 1
But django was searching for old table name in migration file 3

So I renamed table manually to old name, and applied migration and It was successful

0👍

I was also facing the same issue, I’d already created the table but it was still showing that the table does not exist.

A simple solution to this is, Just to delete the database of your PostgreSQL and all migration files in your Django project. After deleting run

python manage.py makemigrations app_name
python manage.py migrate

-1👍

Error is basically because db (postgres or sqlite) have not found the relation, for which you are inserting or else performing CRUD.
The solution is to make migrations

python manage.py makemigrations <app_name>
python manage.py migrate

Leave a comment