31

I'm getting the following error during migration:

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

  File "/Users/user/Documents/workspace/api/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/user/Documents/workspace/api/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/user/Documents/workspace/api/env/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/user/Documents/workspace/api/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)

This is my model:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from ..managers.user import UserManager


class User(AbstractBaseUser, PermissionsMixin):
    # Email identifier, primary key, unique identifier for the user.
    email = models.EmailField(verbose_name='email address', max_length=254, unique=True, db_index=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = 'User'
        app_label = "users"

    def __unicode__(self):
        return self.email

    @property
    def get_full_name(self):
        return self.email

    @property
    def get_short_name(self):
        return self.email

    def has_module_perms(self, app_label):
        """
        Does the user have permissions to view the app `app_label`
        """
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        # Simplest possible answer: All admins are staff
        return self.is_admin

Settings:

AUTH_USER_MODEL = 'users.User'

Anything I have missed?

che
  • 12,097
  • 7
  • 42
  • 71
Prometheus
  • 32,405
  • 54
  • 166
  • 302

4 Answers4

49

Inside your user app, you should have a folder migrations. It should only contain 0001_initial.py and __init__.py. Is that correct?

Try running ./manage.py sqlmigrate user 0001_initial and see what it does, because thats where the error comes from

Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
  • 18
    This was my issue - I had never run the actual initial User migration. You might have not run `python manage.py makemigrations appname`yet - once you do that and `migrate` after, everything should be fine. – sofly Jun 20 '16 at 17:17
  • Running first ./manage.py migrate user 0001_initial worked fine. – André Duarte Mar 20 '20 at 17:37
2

Another issue can also be the fact that you were using the database for another application. So if this can be the case just drop the database before making migrations again

Kelvin Onkundi
  • 139
  • 2
  • 2
  • I literally sqlflushed my db then dropped it and recreated another one with the same properties and then `./manage.py migrate` worked none of the above answers helped – BGOPC Jan 27 '23 at 12:57
0

I had the same issues, and what I did was to run python manage.py makemigrations <app_name> and do not forget to add <app_name> to INSTALLED_APPS in your settings.py file

helseroc
  • 1
  • 1
0

I've also encountered with the same issue in Postgres DB.

Steps to follow:

  • remove previous db and create new one
  • add migration folder and add init.py empty file inside migration folder of each app having models
  • now use command python manage.py makemigrations
  • use command python manage.py migrate
Mayur Gupta
  • 303
  • 2
  • 14