python – ProgrammingError:关系’blah blah’不存在,尝试运行特定的迁移并获得错误
作者:互联网
我收到一个错误:
$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生产)
解决方法:
回溯向您显示在IndexView中发生错误.您正在尝试在加载视图时在数据库中创建对象.
class IndexView(TemplateView):
newhit = HitCounter.objects.create() # remove this line
...
当视图加载时访问数据库是一个坏主意,所以你应该删除该行.在生产中,它会为您提供错误,因为它在您应用创建表的迁移之前尝试在数据库中创建对象.
标签:python,django,postgis,database-migration 来源: https://codeday.me/bug/20190929/1834031.html