django 1.8无法访问django.db.utils.ProgrammingError:relation";“授权用户”;不存在
我有一个Django1.7的工作项目,现在我把它移到了Django1.8。 我可以执行django 1.8无法访问django.db.utils.ProgrammingError:relation";“授权用户”;不存在,django,django-models,Django,Django Models,我有一个Django1.7的工作项目,现在我把它移到了Django1.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, i
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
这也失败了:
django.db.utils.ProgrammingError: relation "django_site" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1
请帮我解决这个问题。我也有同样的问题,我花了好几个小时绞尽脑汁试图找到一个解决方案,这个方案隐藏在评论中。我的问题是CircleCI因为这个错误而无法运行测试。我想我需要用一个新的空数据库重新开始。但我也犯了同样的错误。一切似乎都与“auth”、“contenttypes”和“sites”有关 我读书,也读书。对我来说没有解决办法 因此,在销毁了我的数据库并创建了一个新的数据库之后,我找到的唯一完全避免这些
django.DB.utils.ProgrammingError
的解决方案是:
用户
模型相关的所有代码/manage.py migrate
(之前没有假签名,没有假首字母,没有“auth”或“contenttypes”的迁移,只需简单迁移即可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>',
)
已安装的应用程序=(
“django.contrib.admin”,
“django.contrib.contenttypes”,
“django.contrib.sites”,
“django.contrib.auth”,
“django.contrib.sessions”,
“django.contrib.messages”,
“django.contrib.staticfiles”,
“rest_框架”,
“mptt”,
“djangobower”,
“蜜罐”,
“django_主持人”,
“传单”,
“多选字段”,
“护卫舰”,
“休息时大摇大摆”,
“阿拉斯”,
“allauth.账户”,
#“allauth.socialaccount”,
#“allauth.socialaccount.providers.twitter”,
#“allauth.socialaccount.providers.facebook”,
“项目”,
)
我不喜欢注释/取消注释代码的想法,所以我尝试了另一种方法:我“手动”迁移了一些应用程序,然后对其余的应用程序运行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\u CUSTOM\u USER\u APP
是包含我在我的settings
文件中设置的AUTH\u USER\u model
模型的应用程序的名称
希望能有所帮助。顺便说一句,在Django 1.8中同步数据库的最佳方法如此复杂,这似乎很奇怪。我想知道我是否遗漏了什么(我对Django 1.8不太熟悉,我以前使用的是旧版本)始终使用
python manage.py makemigrations
迁移数据库,然后在较新版本中使用python manage.py migrate
。对于上述错误,如果您是第一次迁移数据库,请使用python manage.py migrate--false initial
。请参阅使用Django 1.10的文档,我找到了另一个解决方案:
我的应用程序名为“web”,首先我调用:
python manage.py makemigrations web
然后我叫:
python manage.py makemigrations auth
python manage.py migrate
然后我叫:
python manage.py makemigrations auth
python manage.py migrate
惊奇:它在工作!:)
似乎auth正在搜索auth_USER_模型“web.UserProfile”和一个名为web_USER_profile的关系,但没有找到它,因此出现了错误。
另一方面,调用makemigrations web首先创建所需的关系,然后auth才能检查并警告它不存在。错误基本上是因为db(postgres或sqlite)没有找到要插入或执行CRUD的关系。 解决方案是进行迁移
python manage.py makemigrations
python manage.py migrate
在我的例子中,当postgresql驱动程序能够连接到数据库时出现此错误,但提供的用户无权访问架构或表等。显示的错误不是说权限被拒绝,而是说找不到正在查询的数据库表。通常在这种情况下,migrate命令在尝试创建django_migrations
表时也会失败,并出现类似错误
检查您在Django数据库连接中使用的用户是否被授予访问权限。删除迁移文件、关联的.pyc文件,为了安全起见,使用以下命令删除所有.pyc文件并不能解决我的问题
$ find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
$ find . -path "*/migrations/*.pyc" -delete
$ find . -name "*.pyc" -exec rm -- {} +
最终解决我的问题的不是清除缓存,而是因为我有一个函数,该函数作为默认函数参数执行查询。在init上,这是像makemigrations
和migrate
这样的命令在执行之前所做的,它看起来像是django(可能是python属性?)初始化了所有默认参数
当下面的默认参数初始化时,由于我的数据库是完全空的(我需要执行migrate--run syncdb
来重新创建表),所以它对空数据库运行了一个查询,随后失败
更改此项:
def generate_new_addresses(number=1, index=None, wallet=get_active_wallet()):
...
...
return
致:
我也有同样的问题,但根本原因是其中一个迁移文件夹中的
\uuu init\uuuuupy.py
文件已从源代码中删除,但未在本地删除(导致“不在我的机器上”错误)
迁移文件夹仍然需要
\uuuu init\uuuu.py
文件,即使使用Python 3也是如此 我在forms.ChoiceForm
queryset中遇到了这个问题。我可以切换到使用延迟评估的数据库,这为我解决了问题。然后,如果您切换回sqlite作为数据库后端,并从空数据库开始,这可能与Django 1.8中的更改有关:如果表已经存在,则在迁移时尝试使用“--false initial”选项;它过去是隐式的,但现在已经显式了:您是否尝试删除SQLite DB或PostgreSQL DB?是的。我已尝试删除数据库。当我运行python manage.py migrate--fake initial myapp
时,它会给我一个错误:应用程序没有