【问题标题】:Exception Type: OperationalError - Exception Value: no such column: blog_article.slug异常类型:OperationalError - 异常值:没有这样的列:blog_article.slug
【发布时间】:2016-06-25 18:39:30
【问题描述】:

我正在使用 django 制作博客。我创建了一个名为 blog 的应用程序,并在 models.py 中为该博客上的文章创建了以下模型:

class Article(models.Model):
    title = models.CharField(max_length = 100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    author = models.CharField(max_length = 100)
    COUNTRIES = (
        ("fr", "france"),
        ("ge", "germany"),
        )
    nationality = models.CharField(choices= COUNTRIES, max_length = 20)
    publication_date = models.DateTimeField()
    content = models.TextField()

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_article', None, { 'slug': self.slug })

在 admin.py 中,我做了以下操作,以便在 slug 中预先填充标题:

class ArticleAdmin(admin.ModelAdmin):
    exclude = ['posted']
    prepopulated_fields = {'slug': ('title',)}

我已经多次运行 python manage.py makemigrations 和 migrate,试图压缩迁移,正如我在此处的其他帖子中发现的那样。当我运行 makemigrations 时,我得到:“未检测到更改”。当我运行 migrate 时,我得到“没有要应用的迁移”。

但是,当我runserver 并进入管理界面以添加新文章时,填写“title”字段,看到自动填充的 slug 字段(由于预填充)并想要保存,这是我得到的:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog/article/add/

Django Version: 1.9.4
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog.apps.BlogConfig']
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.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1370.             if form.is_valid():

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid
  161.         return self.is_bound and not self.errors

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in errors
  153.             self.full_clean()

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean
  364.         self._post_clean()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean
  402.             self.validate_unique()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in validate_unique
  411.             self.instance.validate_unique(exclude=exclude)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in validate_unique
  922.         errors = self._perform_unique_checks(unique_checks)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _perform_unique_checks
  1017.             if qs.exists():

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in exists
  651.             return self.query.has_results(using=self.db)

File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in has_results
  501.         return compiler.has_results()

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in has_results
  819.         return bool(self.execute_sql(SINGLE))

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  848.             cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  323.         return Database.Cursor.execute(self, query, params)

**Exception Type: OperationalError at /admin/blog/article/add/
Exception Value: no such column: blog_article.slug**

如果我在管理界面中单击“文章”,我会得到相同的结果。我试图压制迁移,进行迁移并再次迁移,但它没有奏效......我到处寻找并没有找到适合我的答案 - 如果你能提供一些提示,非常感谢你!

【问题讨论】:

  • Django 版本?你试过python manage.py makemigrations blog,python manage.py migrate blog吗?
  • Django 版本是:(1, 9, 4, 'final', 0) 是的,我也尝试过这些命令,但它们并没有改变结果!

标签: python django database-migration slug


【解决方案1】:

您可以尝试以下方法吗?

检查您的迁移文件夹。假设你在my_project/blog/migrations/0020_...下添加了文章,那么你可以尝试运行吗:

./manage.py migrate blog 0019 => 这应该会给你一个错误(blog_article 表不存在 - 无法删除)

然后伪造它: ./manage.py migrate blog 0019 --fake

然后再次迁移: ./manage.py migrate blog

【讨论】:

  • 非常感谢您的回答。不幸的是,我已经尝试过了,但没有成功!我之前已经删除了所有迁移并再次 makemigrations/migrate,以防迁移文件夹出现问题(并且因为我的应用程序处于开发的早期阶段,这意味着我不介意删除迁移)......这里是我唯一的迁移文件的样子(除了 init.py):
  • 类迁移(migrations.Migration):初始=真正的依赖=[]操作=[migrations.CreateModel(name='Article',fields=[('id',models.AutoField(auto_created= True, primary_key=True, serialize=False, verbose_name='ID')), ('title', models.CharField(max_length=100, unique=True)), ('slug', models.SlugField(max_length=100, unique=True)), ('author', models.CharField(max_length=100)), ('content', models.TextField()), ], ),
  • 好的,所以这可能不是迁移问题。您能否确认该表是使用其所有列创建的(使用原始 SQL)?如果你用prepopulated_fields注释掉这行,管理页面是什么样子的?
  • 也许在管理员中尝试明确列出字段 - 而不是使用exclude。
  • 是否有可能与另一个“博客”应用程序发生冲突?
猜你喜欢
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2015-04-21
  • 2016-04-05
  • 1970-01-01
相关资源
最近更新 更多