相关文章推荐
坏坏的西红柿  ·  Django 教程 11:部署 ...·  2 周前    · 
酒量小的葡萄酒  ·  Visual Studio 中的 ...·  2 周前    · 
满身肌肉的包子  ·  SQL 高级查询 ...·  1 年前    · 
飘逸的核桃  ·  Android 错误集合 - ...·  2 年前    · 
潇洒的大海  ·  SQL ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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'

Also 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"

If I am doing 'migrate', I see something like this

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

I tried to add 'blank = True, null = True', but it's didn't help me. How can I fix this problem?

  • python manage.py migrate --fake

  • comment color_set

  • python manage.py makemigrations && python manage.py migrate
  • uncomment color_set
  • python manage.py makemigrations && python manage.py migrate
  • The problem is because you added null=True after creating the migration file. Follow these steps now.

    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'
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.