Django错误:关系"users_user"不存在

dgjrabp2  于 2023-01-31  发布在  Go
关注(0)|答案(4)|浏览(151)

我在迁移过程中收到以下错误:
django.db.utils.ProgrammingError:关系"users_user"不存在

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)

这是我的模型:

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

设置:

AUTH_USER_MODEL = 'users.User'

我错过什么了吗?

xriantvc

xriantvc1#

在您的用户应用中,您应该有一个文件夹migrations。它应该只包含0001_initial.py__init__.py。对吗?
试着运行./manage.py sqlmigrate user 0001_initial,看看它做了什么,因为这就是错误的来源

9lowa7mx

9lowa7mx2#

另一个问题也可能是您正在为另一个应用程序使用数据库,因此如果是这种情况,只需在再次迁移之前删除数据库即可

wnrlj8wa

wnrlj8wa3#

我遇到了同样的问题,我所做的是运行python manage.py makemigrations <app_name>,并且不要忘记在settings.py文件中将<app_name>添加到INSTALLED_APPS

wgx48brx

wgx48brx4#

我在Postgres DB中也遇到过同样的问题。
应遵循的步骤:

  • 删除以前的数据库并创建新数据库
  • 添加migration文件夹,并在具有模型的每个应用程序的migration文件夹中添加****init**. py**空文件
  • 现在使用命令python manage.py makemigrations
  • 使用命令python manage.py migrate

相关问题