【问题标题】:Django Make Migration Error No Such ColumnDjango 使迁移错误没有这样的列
【发布时间】:2017-12-10 08:33:27
【问题描述】:

我有一个预先存在的模型,现在我也在尝试添加一个额外的字段。不是我制作了原始字段,但我一直在向其他类添加字段并使迁移正常。

我想向 Event 类添加一个额外的图像字段,该类上已经有一个图像字段,所以我知道它可以处理图像字段,我尝试更改名称以查看这是否也是一个问题。这是我要添加的字段:

social_media_image = models.ImageField(null=True, blank=True, help_text='Hello World')

这是我在将该代码添加到模型后尝试进行迁移时遇到的错误:

django.db.utils.OperationalError: no such column: posts_event.social_media_image

根据我对迁移工作原理的理解,不应该有一个称为此的列,因为我还没有进行迁移,它将把它添加到数据库中。

我也尝试向该字段添加默认值,但没有成功。我还尝试完全删除数据库以及迁移文件并尝试重新创建它们。

以下是模型中的其他字段:

slug = AutoSlugField(max_length=50, unique=True, populate_from='title')
content = models.TextField(default='')
start_time = models.DateTimeField()
image = models.ImageField(null=True, blank=True, help_text=image_help_text)

这是回溯:

Traceback (most recent call last):
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: posts_event.social_media_image

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/apps.py", line 37, in ready
    search.register(Event.objects.get_upcoming_events(site_id=settings.SITE_ID, include_spektrix=False, return_queryset=True))
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/models.py", line 282, in get_upcoming_events
    events_with_a_next_start_time = [e for e in events if e.next_start_time() is not None]
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: posts_event.social_media_image

【问题讨论】:

  • 你能多展示一些模型吗?
  • 添加字段后运行什么命令?
  • 您提交的图片丢失 upload_to=upload_location。只是说。所以为了弄清楚这一点,你运行./manage.py makemigrations 然后./manage.py migrate 对吗?而且您不必完全删除数据库。下次回滚stackoverflow.com/questions/44182633/…
  • 显示所有模型字段,您是否为此模型编写了任何表单?
  • 我已经用这个班的其他模型更新了这个问题。 @hansTheFranz 我运行 makemigrations 并得到该错误。看到使迁移失败运行迁移不会有任何影响

标签: python django django-models django-migrations


【解决方案1】:

此问题可能是由于数据库中可能不存在相应的图像字段。请登录数据库shell并检查相应的字段是否存在。如果不存在,则使用 SQL 查询手动添加它或回滚到以前的安全迁移,然后在 models.py 中进行更改并生成迁移文件。

【讨论】:

    【解决方案2】:

    就在那儿:

    File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/apps.py", line 37, in ready:
    search.register(Event.objects.get_upcoming_events(site_id=settings.SITE_ID, include_spektrix=False, return_queryset=True))
    

    您对一个应用程序的源执行查询集,据我所知,它会查询另一个应用程序的模型。如果可以避免,则不应在应用设置中进行查询。必要时使用信号或延迟加载。

    如果您确实需要,请翻转应用程序的顺序。如果它是同一个应用程序,那么是的,不要这样做:如您所见,您将无法进行任何迁移。

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 2020-04-15
      • 2011-05-06
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多