django 1.8无法django.db.utils.ProgrammingError:关系“auth_user”不存在

问题描述 投票:17回答:8

我有一个django 1.7的工作项目,现在我把它移到django 1.8。我可以做syncdb并使用sqlite运行应用程序,但是当我切换到postgres时,它无法执行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

我尝试删除数据库并重新创建它。另外,我试过:

python manage.py migrate auth

也失败了:

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

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

请帮忙解决这个问题。

django django-models
8个回答
11
投票

我不喜欢评论/取消注释代码的想法,所以我尝试了一种不同的方法:我“手动”迁移了一些应用程序,然后为其余的应用程序运行django-admin.py migrate。删除所有*.pyc文件后,我的命令序列是:

$ 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

其中MY_CUSTOM_USER_APP是包含我在AUTH_USER_MODEL文件中设置settings的模型的应用程序的名称。

希望它可以提供帮助。顺便说一下,在Django 1.8中同步你的数据库的最佳方法是如此复杂,这似乎很奇怪。我想知道我是否遗漏了一些东西(我不熟悉Django 1.8,我曾经使用旧版本)


7
投票

始终使用python manage.py makemigrations迁移db,然后在较新版本中迁移python manage.py migrate。对于上述错误,如果您第一次迁移数据库,请使用python manage.py migrate --fake-initial。请参阅docs https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-migrate


5
投票

在Django 1.10上工作我找到了另一个解决方案:我的应用程序名为“web”,我首先调用:

python manage.py makemigrations web

然后我打电话给:

python manage.py makemigrations auth

然后我打电话给:

python manage.py migrate

惊讶:它在工作! :)似乎auth正在搜索AUTH_USER_MODEL“web.UserProfile”和一个名为web_user_profile的关系,它没有找到它,因此错误。另一方面,在auth能够检查并警告它不存在之前,首先调用makemigrations web首先创建所需的关系。


4
投票

我遇到了同样的问题,我花了好几个小时才想找到一个解决方案,这个解决方案隐藏在评论中。我的问题是CircleCI因为这个错误而无法运行测试。而且我认为我需要从一个新的空DB开始。但我得到了同样的错误。一切似乎与'auth','contenttypes'和'sites'有关。

我读了thisthis,以及thisthis。对我来说没有解决方案。

因此,在销毁了我的数据库并创建了一个新数据库后,我发现完全避免这些django.db.utils.ProgrammingError的唯一解决方案是:

  1. 注释掉与User模型相关的所有代码。
  2. 删除项目中的所有.pyc文件! find . -name "*.pyc" -exec rm -- {} +谢谢@max!
  3. 运行./manage.py migrate(没有假的,没有假的初始,没有'auth'或'contenttypes'的迁移之前,juste普通迁移。
  4. 取消注释上面的代码,然后再次运行迁移!

我的INSTALLED_APP如下:

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

1
投票

在我的情况下,当postgresql驱动程序能够连接到数据库时出现此错误,但提供的用户无权访问架构或表等。而不是说拒绝权限,显示的错误是说找不到正在查询的数据库表。通常在这种情况下,当尝试创建django_migrations表时,migrate命令也会失败并出现类似的错误。

检查您在Django中使用数据库连接时正在使用的用户的访问权限。


1
投票

我遇到了同样的问题,但我的根本原因是其中一个迁移文件夹中的__init__.py文件已从源代码中删除但未在本地删除(导致“Not on my machine”错误)。

迁移文件夹仍然需要__init__.py文件,即使使用Python 3也是如此。


0
投票

使用以下命令删除迁移文件,关联的.pyc文件以及安全的所有.pyc文件并没有解决我的问题。

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

最终解决我的问题的不是清除缓存,而是因为我有一个函数执行查询作为默认函数参数。在init上,这就像makemigrationsmigrate这样的命令在执行之前做的似乎是django(也许是python属性?)初始化所有默认参数。

由于我的数据库完全为空(我需要执行migrate --run-syncdb来重新创建表),当初始化下面的默认参数时,它会针对随后失败的空数据库运行查询。

改变这个:

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

至:

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

-1
投票

错误基本上是因为db(postgres或sqlite)没有找到您要插入或执行CRUD的关系。解决方案是进行迁移

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

© www.soinside.com 2019 - 2024. All rights reserved.