ProgrammingError: relation 'blah blah' doe

2019-01-29 09:14发布

问题:

I am getting an error:

$ 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"...

This is confusing to me as it is specifically trying to build the swsite_hitcounter table as in the following migration I am trying to fun:

# -*- 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()),
            ],
        ),
    ]

Is something maybe out of sink? This is me trying to run this migration on my production box, it ran fine (of course) on my development box. Might of been cause I ran a specific migration? :

python manage.py migrate swsite 0023_hitcounter.py

Though running:

python manage.py showmigrations

Gives me same error? I am guessing my migrations are out of sync but not being able to show migrations, I don't know how to show the ones that didn't get into this update (on production from gilab)

回答1:

The traceback is showing you that the error is occuring in IndexView. You are trying to create objects in the database when the view loads.

class IndexView(TemplateView):
    newhit = HitCounter.objects.create()  # remove this line
    ...

Accessing the database when the views load like this is a bad idea, so you should probably remove the line. In production, it gives you the error because it is trying to create the object in the database before you have applied the migration that creates the table.