ProgrammingError: 关系 'blah blah' 不存在,试图运行特定的迁移并得到错误
Posted
技术标签:
【中文标题】ProgrammingError: 关系 \'blah blah\' 不存在,试图运行特定的迁移并得到错误【英文标题】:ProgrammingError: relation 'blah blah' does not exist, trying to run the specific migration and get errorProgrammingError: 关系 'blah blah' 不存在,试图运行特定的迁移并得到错误 【发布时间】:2017-12-22 13:09:05 【问题描述】:我收到一个错误:
$ python manage.py migrate swsite 0023_hitcounter.py
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/lib64/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/lib64/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/lib64/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/lib64/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib64/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib64/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib64/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/swlab-website/mysite/urls.py", line 25, in <module>
url(r'^swsite/', include('swsite.urls')),
File "/usr/lib64/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/swlab-website/swsite/urls.py", line 2, in <module>
from . import views
File "/var/www/swlab-website/swsite/views.py", line 27, in <module>
class IndexView(generic.ListView):
File "/var/www/swlab-website/swsite/views.py", line 31, in IndexView
newhit = HitCounter.objects.create()
File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib64/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "swsite_hitcounter" does not exist
LINE 1: INSERT INTO "swsite_hitcounter" ("date", "template_location"...
这让我感到困惑,因为它专门尝试构建 swsite_hitcounter 表,就像我想玩的以下迁移一样:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2017-07-05 15:56
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('swsite', '0022_auto_20170307_1343'),
]
operations = [
migrations.CreateModel(
name='HitCounter',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(auto_now=True)),
('template_location', models.TextField()),
],
),
]
是不是有什么东西不在水槽里了?这是我试图在我的生产机器上运行此迁移,它(当然)在我的开发机器上运行良好。可能是因为我进行了特定的迁移? :
python manage.py 迁移 swsite 0023_hitcounter.py
虽然正在运行:
python manage.py showmigrations
给我同样的错误?我猜我的迁移不同步但无法显示迁移,我不知道如何显示未进入此更新的迁移(在 gilab 的生产中)
【问题讨论】:
这里有同样的问题 :( 我查看了 django_migrations 表以了解我的开发以及我正在尝试将其推向生产的表。这些表有很大的不同:/我知道我做了一些骇客……但是该死的。我希望我可以让我的开发和生产设备恢复到相同的迁移级别,而不会丢失生产数据。 【参考方案1】:回溯显示IndexView
中发生了错误。当视图加载时,您正在尝试在数据库中创建对象。
class IndexView(TemplateView):
newhit = HitCounter.objects.create() # remove this line
...
像这样加载视图时访问数据库是个坏主意,因此您可能应该删除该行。在生产中,它会给您错误,因为它试图在您应用创建表的迁移之前在数据库中创建对象。
【讨论】:
关于此处的信息,我将其标记为已回答。我在错误中完全错过了它。我不得不问,为什么这是一个坏主意。该代码是一个简单的片段,它向表中添加一个值作为超级简单的命中计数器。如果我不在视图中执行此操作,我不确定其他人在哪里/如何执行此操作 我已经看到模块级数据库查询会导致陈旧数据出现问题,我认为甚至会在运行测试时导致问题。我不知道你的命中计数器是如何工作的,所以我不知道最好的位置 - 最好作为一个单独的问题。以上是关于ProgrammingError: 关系 'blah blah' 不存在,试图运行特定的迁移并得到错误的主要内容,如果未能解决你的问题,请参考以下文章
django.db.utils.ProgrammingError: 关系已经存在于 OenBSD vps
/ 关系“main_post”处的 ProgrammingError 不存在
django.db.utils.ProgrammingError:关系“users_user”不存在
解决“django.db.utils.ProgrammingError: 关系 django_migrations 的权限被拒绝”的步骤
使用 South 在 Heroku 上设置 Django 时遇到问题 - 不断出现 ProgrammingError:关系不存在