gpt4 book ai didi

python - django.db.utils.IntegrityError : column "color_set_id" contains null values

转载 作者:行者123 更新时间:2023-12-01 19:45:27 29 4
gpt4 key购买 nike

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

我尝试添加“blank = True,null = True”,但这对我没有帮助。我该如何解决这个问题?

最佳答案

按照以下步骤操作

  • python manage.py migrate --fake

  • 评论color_set

  • python 管理.py makemigrations && python 管理.py 迁移
  • 取消注释color_set
  • python 管理.py makemigrations && python 管理.py 迁移

关于python - django.db.utils.IntegrityError : column "color_set_id" contains null values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277395/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com