django 1.9:ProgrammingError:关系“users_user”不存在

时间:2016-06-01 17:30:24

标签: django django-allauth

我在进行迁移时遇到了ProgrammingError,我认为这可能与django-allauth与自定义用户的使用有关。这是我做的事情

1 /使用psql创建一个新数据库:

create database dj_example;

2 / Installed_apps包含django.contrib.sites:

DJANGO_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
)
THIRD_PARTY_APPS = (
'crispy_forms',  # Form layouts
'allauth',  # registration
'allauth.account',  # registration
#'allauth.socialaccount',  # registration
#'allauth.socialaccount.providers.twitter',
'djcelery', #Celery
)
LOCAL_APPS = (
'tucat.users',  # custom users app
}

3 / site_id设置为1

SITE_ID = 1

4 /自定义用户模型过于简单:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
  def __unicode__(self):
    return self.username

5 / Makemigrations工作正常

# python manage.py makemigrations
Migrations for 'djcelery':
  0028_auto_20160601_1919.py:
  - Alter field status on taskmeta

6 / Migrate返回 ProgrammingError:关系“users_user”不存在

# python manage.py migrate
  Operations to perform:
  Apply all migrations: auth, contenttypes, djcelery, account, admin, sessions
  Running migrations:
  Rendering model states... DONE
  Applying account.0001_initial...Traceback (most recent call last):
  File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "users_user" does not exist

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 12, in <module>
  execute_from_command_line(sys.argv)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
  utility.execute()
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
  self.execute(*args, **cmd_options)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
  output = self.handle(*args, **options)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
  executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
  self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
  state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
  state = migration.apply(state, schema_editor)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 90, in __exit__
  self.execute(sql)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
  cursor.execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
  return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
  raise value.with_traceback(tb)
File "/home/antoinet/.virtualenvs/tucat/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
  return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_user" does not exist

知道如何解决这个问题?

3 个答案:

答案 0 :(得分:9)

您必须删除迁移文件夹,然后才能删除

python manage.py migrate --run-syncdb

python manage.py migrate --fake appname

答案 1 :(得分:5)

您的错误是由运行迁移的顺序引起的。由于许多应用依赖于现有的用户模型,因此您必须在其他应用之前为自定义用户应用运行初始迁移。

如果更改现有项目中的默认用户模型,则可能更容易丢弃所有现有迁移(和数据库)并从头开始重建。应用迁移的顺序为:

  1. 核心django.contrib个应用。
  2. 您的自定义用户应用
  3. 其他自定义应用和第三方应用。
  4. 您可以使用django-admin showmigrations查看哪些迁移存在且已计划好。

答案 2 :(得分:0)

FYI -

我遇到了这个问题,为了解决这个问题,我必须在urls.py和url_tenants.py文件中注释掉所有对views.py的引用。然后,我运行makemigrations并创建数据库表,然后运行migrate_schemas,稍后取消注释url文件。希望这有助于某人。