django.db.utils.IntegrityError:“ color_set_id”列包含空值 - python

app.models.py

class ColorSet(models.Model):
    color = models.CharField(max_length=50, verbose_name='Color', default='', blank=True, null=True)
    code = models.CharField(max_length=50, verbose_name='Code of color', default='', blank=True, null=True)

    class Meta:
        verbose_name = 'Color'
        verbose_name_plural = 'Colors'

也是project.models.py

class Product(models.Model):
    title = models.CharField(max_length=200, verbose_name='Title of product')
    slug_field = models.SlugField(max_length=50, verbose_name='Slug', default='')
    description = models.TextField(verbose_name='Description')
    color_set = models.ForeignKey(ColorSet, verbose_name='Color', default='', blank=True, null=True)

    def __str__(self):
        return '%s %s %s' % (self.title, '->', self.category)

    class Meta:
        verbose_name = "Product"
        verbose_name_plural = "Products"

如果我在做“移民”,我会看到类似的东西

Traceback (most recent call last):
File "manage.py", line 22, in <module>
  execute_from_command_line(sys.argv)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
  utility.execute()
File "/home/vladislav/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
  self.execute(*args, **cmd_options)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
  output = self.handle(*args, **options)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
  fake_initial=fake_initial,
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
  state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
  state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
  state = migration.apply(state, schema_editor)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
  operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 87, in database_forwards
  field,
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 429, in add_field
  self.execute(sql, params)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
  cursor.execute(sql, params)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
  return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
  return self.cursor.execute(sql, params)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
  raise value.with_traceback(tb)
File "/home/vladislav/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
  return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "color_set_id" contains null values

我试图添加“空白= True,null = True”,但这并没有帮助我。
我该如何解决这个问题?

参考方案

问题是因为创建迁移文件后添加了null = True。立即执行以下步骤。

1) Drop ColorSet & Product tables from your local table database.
2) Delete all the migration files you created for these `CharField to a ForeignKey` change
3) execute `python manage.py makemigrations`
4) execute 'python manage.py migrate'

Django TestCase不保存我的模型 - python

我目前正在为Django应用编写一些测试。我的应用程序的signal.py文件中具有以下独立功能:def updateLeaveCounts(): # Setting some variables here todaysPeriods = Period.objects.filter(end__lte=today_end, end__gte=today_sta…

Django 1.8-如何测试tests文件夹中的特定文件? - python

这是我的目录:CMSApp/tests/test_page.py CMSApp/tests/test_user.py CMSApp/models.py CMSApp/views.py 我只想测试test_page.py。我可以做这个:python manage.py test CMSApp/tests 但这将同时测试test_page.py和test_use…

处理后如何删除照片? - python

我的Django模型中有两个字段: class Staff(models.Model): photo = models.FileField(blank=True, null = True) encodings = JSONField() 我从表单获取照片,然后使用该照片获取编码。处理后如何删除照片?我试过了self.photo = None or self.…

django-compressor未与django-shop一起安装 - python

我无法使用django-shop安装django-compressor。出现这样的错误。Failed building wheel for rcssmin ================================= Failed building wheel for rjsmin -----------------------------------…

如何在Django中解决模块名称冲突? - python

创建Django应用程序时出错:python端发生错误。退出代码:1,err:CommandError:'untitled1'与现有Python模块的名称冲突,因此不能用作项目名称。请尝试使用其他名称。 python大神给出的解决方案 您正在使用哪个版本的python?升级您的django版本或降级您的python版本,这应该可以解决问题。您可以在cl中执行…