Django:向模型添加列,给出列不存在的错误

时间:2018-01-16 18:28:00

标签: python django database models

我有两个模型,每个模型都在相同名称的不同应用中:

class Vendor(models.Model):
    name = models.CharField(max_length=200, blank=True, default='DEFAULT')
    serial_num = models.CharField(max_length=200, blank=True)

class Node(models.Model):
    name = models.CharField(max_length=200, blank=True)
    persons = models.ForeignKey(Person, on_delete=models.SET_NULL, null=True)

当我尝试python3 manage.py makemigrations时,我会收到下面的冗长错误。我认为问题可能是迁移文件。因为Home具有Person的外键,0001_initial.py具有依赖('Person', '0007_auto_20180110_1827'),,并且我无法将serial_num列添加到Person。因此,我尝试删除位置列并重新添加位置列,然后收到以下错误,指出它无法识别列location

如何解决此问题?另外,如果我需要提供有关该问题的更多信息,请与我们联系。

Traceback (most recent call last):
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: vendor_vendor.location

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/QuikBikes/urls.py", line 28, in <module>
    path('node/', include('node.urls')),
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/urls.py", line 3, in <module>
    from . import views
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/views.py", line 9, in <module>
    from .forms import AddNodeForm
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/forms.py", line 7, in <module>
    class AddNodeForm(forms.Form):
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/forms.py", line 10, in AddNodeForm
    for person in Vendor.objects.all():
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
    cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: vendor_vendor.location
(QuikBikes) Karthiks-MacBook-Pro:QuikBikes karthikpullela$ python3 manage.py makemigrations
Traceback (most recent call last):
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: vendor_vendor.location

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/QuikBikes/urls.py", line 28, in <module>
    path('node/', include('node.urls')),
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/urls.py", line 3, in <module>
    from . import views
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/views.py", line 9, in <module>
    from .forms import AddNodeForm
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/forms.py", line 7, in <module>
    class AddNodeForm(forms.Form):
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/QuikBikes/node/forms.py", line 10, in AddNodeForm
    for person in Vendor.objects.all():
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
    cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/karthikpullela/Desktop/Django-projects/QuikBikes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: vendor_vendor.location

编辑:这是节点的forms.py

from django import forms
from django.contrib.auth.models import User, Group
from vendor.models import Vendor

class AddNodeForm(forms.Form):

    VENDOR_CHOICES = [('none', '---------')]
    for person in Vendor.objects.all():
        VENDOR_CHOICES.append((person, person))

    node_vendor = forms.ChoiceField(label='Vendor', choices=VENDOR_CHOICES)

1 个答案:

答案 0 :(得分:1)

您正在执行查询的AddNodeForm中的类级别有一些代码。由于该代码在导入时被调用,因此在迁移甚至可能有机会运行之前执行它。

您不应该在该级别进行查询 - 或者确实是任何重要的逻辑。这样的事情应该在方法中。