Why I am getting django.db.utils.IntegrityError even after removing default=0?
I am new to Django. I was working on a project whose one of the database tables has a recursive foreignkey. I am sharing that table here.
class Categories(models.Model):
name = models.CharField(max_length=25)
slug = models.CharField(max_length=25)
parent = models.ForeignKey("self",on_delete=CASCADE,default=None,blank=True)
My database migration did not work when the default was 0. It worked fine when there was null = True
. The problem occurred when I removed the null and set default to 0. After having the error, I removed the default=0
and set the null to true. Whatever I try I still have the same problem when I run the python manage.py migrate
command.
This is my problem:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, stock_app
Running migrations:
Applying stock_app.0005_alter_categories_parent...Traceback (most recent call last):
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.ForeignKeyViolation: insert or update on table "stock_app_categories" violates foreign key constraint "stock_app_categories_parent_id_b0ca10d8_fk_stock_app"
DETAIL: Key (parent_id)=(0) is not present in table "stock_app_categories".
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\core\management\commands\migrate.py", line 246, in handle
fake_initial=fake_initial,
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\migrations\operations\fields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\base\schema.py", line 609, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\postgresql\schema.py", line 198, in _alter_field
new_db_params, strict,
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\base\schema.py", line 843, in _alter_field
self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s"))
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\base\schema.py", line 145, in execute
cursor.execute(sql, params)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Rakin Shahriar\Desktop\stock-image-sharing-platform\stock_image_sharing_platform\myenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: insert or update on table "stock_app_categories" violates foreign key constraint "stock_app_categories_parent_id_b0ca10d8_fk_stock_app"
DETAIL: Key (parent_id)=(0) is not present in table "stock_app_categories".
What should I do now?