49

When upgrading to Django 1.8 (with zc.buildout) and running syncdb or migrate, I get this message:

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

One of my models contains django.contrib.auth.models.User:

user = models.ForeignKey(
    User, related_name='%(app_label)s_%(class)s_user',
    blank=True, null=True, editable=False
)

Downgrading to Django 1.7 removes the error. Do I have to include the User object differently in Django 1.8?

ferrangb
  • 1,836
  • 2
  • 16
  • 32
mpso
  • 1,091
  • 1
  • 13
  • 25

8 Answers8

99

I fix this by running auth first, then the rest of my migrations:

python manage.py migrate auth
python manage.py migrate
Dave Lawrence
  • 1,187
  • 7
  • 8
  • 4
    Your auth app should already be near the top in INSTALLED_APPS, i.e. 'django.contrib.admin', if its in the correct order the above shouldn't make a difference. – radtek May 03 '15 at 22:42
  • 10
    I get `relation "django_site" does not exist` . I'm on django 1.8.2. I tried a few different apps in my project and always got the same error, then I ran `python manage.py migrate` again, and lo and behold it worked. – trpt4him Sep 18 '15 at 11:13
19

On my environment, I fix this running makemigrations on all apps that have relationship with django.contrib.auth.models:

manage.py makemigrations app_with_user_relation
manage.py migrate
Pedro Vagner
  • 8,029
  • 3
  • 26
  • 19
  • I had the same issue and this solution worked. Passing from Django 1.7.X to 1.8.1 – Daviddd May 06 '15 at 17:07
  • I can confirm that this still works, and for use with Heroku. – James May 18 '15 at 19:51
  • This solution worked for me in D2.2 for a very non-related issue. Was trying to run unit tests and kept getting this error. Turned out there was a held-up migration in the users app that needed to be run manually (i.e. `makemigrations users`) and suddenly my unit test module started functioning. – D. Simpson Oct 23 '19 at 01:46
8

This problem is contained by running "makemigrations" for all apps, that have not yet been migrated, i.e. apps that do not yet have an "initial_0001.py" file in their migrations directory.

This is done (in our case we use a makefile) by running for each of these apps:

manage.py makemigrations app_name

Once that is done, you can execute:

manage.py migrate

as usual.

The underlying cause for this is that for some reason

manage.py makemigrations

does not always create these initial migrations if they are not already there. And that leads to the mentioned error.

On the contrary,

manage.py makemigrations app_name

does always create them (if not already there). Unfortunately I cannot fathom the reasons for this asymmetry.

Ytsen de Boer
  • 1,966
  • 1
  • 15
  • 25
  • In which directory do you run the command? I get the error "Can't open file manage.py". – Mathias Lykkegaard Lorenzen May 05 '16 at 10:34
  • 1
    You should run that command inside the directory that contains the file "manage.py". You can try the following 2 alternatives: 1) prepend the command with dot-slash, like so: "./manage.py makemigrations" or 2) prepend with the python executable, like so: "python manage.py makemigrations". Good luck. – Ytsen de Boer May 10 '16 at 08:42
  • But where is this located? – Mathias Lykkegaard Lorenzen May 11 '16 at 09:36
  • 1
    In the same directory as where your app directories are located, see here for a directory layout (I cannot paste it here), look for "Creating a project": https://docs.djangoproject.com/en/1.9/intro/tutorial01/. If you don't have it anymore, you should to replace it. Btw.: you can use "django-admin" as well, which should be in your Python path, see also: https://docs.djangoproject.com/en/1.9/ref/django-admin/ – Ytsen de Boer May 14 '16 at 07:34
7

If you are using heroku like I was run

heroku run python manage.py makemigrations

This will likely give you a message saying there are now changes. Ignore that then run

heroku run python manage.py migrate

this will give you some output suggesting something has been done. Finally run

heroku run python manage.py createsuperuser
Aaron F
  • 371
  • 3
  • 7
4

To fix this problem here is what I did:

1) Find all foreign key relation fields like OneToOneField, ForeignKey and ManyToManyFields in your project, including any reusable apps that are referring to auth.User or import User and set it to settings.AUTH_USER_MODEL as above. At minimum use:

'auth.User'

2) For all the models that have the above, make sure the models have a valid django migration (not south). If they have south migrations, rename the directory to migrations_south and then run the makemigrations command for that app:

./manage.py makemigrations affected_app

Sometimes there is a django migrations folder under a different name, not the default migrations directory. In such cases, reference this via MIGRATION_MODULES in your settings.py:

MIGRATION_MODULES = {'filer': 'filer.migrations_django'}

Since the issue was hard to find on larger projects, I commented out all custom apps in INSTALLED_APPS in settings.py and ran the test command, since it will run migrate and try to re-create the database for you:

./manage.py test

Looks like that fixed it for me. I'm not sure if step 1 is mandatory or just best practice. But you definitely need to convert the apps to migrations.

Cheers!

PS. Be ready for what's coming in Django 1.9. The syncdb command will be removed. The legacy method of syncing apps without migrations is removed, and migrations are compulsory for all apps.

vabada
  • 1,558
  • 3
  • 26
  • 34
radtek
  • 26,590
  • 9
  • 126
  • 97
0

Try referring to the User using this

from django.conf import settings
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_user', blank=True, null=True, editable=False)
Rod Xavier
  • 3,753
  • 1
  • 25
  • 39
  • Doesn't work for me. I have no migrations, just trying to run ./manage.py test and the the fail right away when migrate is run: django.db.utils.ProgrammingError: relation "auth_user" does not exist – radtek Apr 22 '15 at 18:31
  • The fix is to have your models that rely on foreign keys to auth_model also under migrations. Good practice overall since syncdb is gone in Django 1.9 – radtek Apr 22 '15 at 21:38
0

I've migrated an old Django 1.6 project to Django 1.8 and previously we've used syncdb to migrate the database and we did not have initial migration steps for all apps in our project. With Django 1.8, you'll need a working database migrations. Running

manage.py makemigrations <app_name>

for all apps in our project fixed our problems.

Sebastian Wagner
  • 1,655
  • 2
  • 16
  • 24
0

Maybe you have found the answer and resolved the problem, but I wanted to point out that, in my case, the above problem was solved by dropping the database and re-creating it again, with the full privileges of the users. I was able to do this because I'm working on a non-production environment, but doing this on a staging environment is not a good idea, so be careful.

Im using python 2.7.12 and following are the specifications of my virtualenv:

Django==1.10.5
django-crispy-forms==1.6.1
django-registration-redux==1.4
djangorestframework==3.5.3
olefile==0.44
packaging==16.8
Pillow==4.0.0
psycopg2==2.6.2
cavpollo
  • 3,391
  • 2
  • 36
  • 53
  • Thank you very much "cavpollo" for the correction. it looks definitely better. – khaled kachkorian Feb 03 '17 at 17:51
  • Dropping and recreating the database was the only thing that worked for me. After that, I ran python manage.py migrate, then the problem was fixed. All I had to do after that was create another superuser. – DevBot Dec 17 '17 at 20:03