Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

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?

like image 477
Just Snake Avatar asked Jan 29 '23 14:01

Just Snake


2 Answers

Follow these steps

  • 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
like image 166
itzMEonTV Avatar answered Feb 02 '23 11:02

itzMEonTV


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'
like image 44
Prakhar Trivedi Avatar answered Feb 02 '23 10:02

Prakhar Trivedi