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',但这对我没有帮助。 我该如何解决这个问题?
答案 0 :(得分:3)
请按照以下步骤操作
python manage.py migrate --fake
评论color_set
color_set
答案 1 :(得分:0)
问题是因为您在创建迁移文件后添加了 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'