首页 > 解决方案 > OperationalError:没有这样的列,但列在任何地方都不存在

问题描述

我正在建立一个 Django 网站,我是一个相对初学者。但是,这个错误让我感到困惑,所以请让我知道可能发生了什么。

我收到一个错误

django.db.utils.OperationalError: no such column: metadata.id

但是在我的代码中没有 metadata.id,也没有在我使用的数据库中。所以我真的很困惑这是从哪里来的。

大概我在不应该的地方进行了修补。

那么我的问题是,这条消息的开头是否表明我应该在哪里寻找错误?

我也有这个消息

In template .../kinbank2/templates/base.html, error at line 0

这使得很难找到要查看的行或文件。

谢谢,

山姆

错误回溯

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/language/

Django Version: 2.0.4
Python Version: 3.5.2
Installed Applications:
['home.apps.HomeConfig',
 'django_tables2',
 'language.apps.LanguageViewConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /Users/sp16194/Desktop/Projects_Git/kinbank2/kinbank2/templates/base.html, error at line 0
   no such column: metadata.id
   1 : <!DOCTYPE html>
   2 : <html>
   3 : <head>
   4 :     <title>kinbank</title>
   5 : <!--      <link rel="stylesheet" href="styles.css"> -->
   6 :     {% block head %}{% endblock %}
   7 : </head>
   8 : <body>
   9 :  <div>
   10 :        <div class="page-width"> 


Traceback:

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py" in execute
  303.         return Database.Cursor.execute(self, query, params)

The above exception (no such column: metadata.id) was the direct cause of the following exception:

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/sp16194/Desktop/Projects_Git/kinbank2/language/views.py" in index
  19.   return render(request, 'language/index.html', context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  313.             if match:

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in __bool__
  276.         self._fetch_all()

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1179.             self._result_cache = list(self._iterable_class(self))

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1067.             cursor.execute(sql, params)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  100.             return super().execute(sql, params)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "/Users/sp16194/anaconda/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py" in execute
  303.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /language/
Exception Value: no such column: metadata.id

** 编辑 **

在 Roy 的搜索和帮助下,我认为这与模型以及它们是如何创建的有关?

当我转到文件 language/migrations/0001_initial.py 时,我有以下与我的模型相关的代码:

migrations.CreateModel(
    name='Metadata',
    fields=[
        ('id', models.AutoField(auto_created=True,primary_key=True, serialize=False, verbose_name='ID')),
        ('kinbank_id', models.TextField(blank=True, null=True, unique=True)),
        ('preferred_name', models.TextField(blank=True, db_column='Preferred_name', null=True)),
        ('language_family', models.TextField(blank=True, db_column='Language_Family', null=True)),
        ('latitude', models.TextField(blank=True, db_column='Latitude', null=True)),
        ('longitude', models.TextField(blank=True, db_column='Longitude', null=True)),
        ('glottolog_id', models.TextField(blank=True, db_column='Glottolog_id', null=True)),
        ('dplace_id', models.TextField(blank=True, db_column='DPLACE_id', null=True)),
        ('iso_code', models.TextField(blank=True, db_column='ISO_code', null=True)),
        ('status', models.TextField(blank=True, db_column='Status', null=True)),
        ('notes', models.TextField(blank=True, db_column='Notes', null=True)),
        ('url', models.TextField(blank=True, null=True)),
    ],
    options={
        'managed': False,
        'db_table': 'metadata',
    },

哪个似乎添加了我没有的 id 列?我应该做些什么来阻止这种情况吗?

标签: pythondjango

解决方案


推荐阅读