Answer the question
In order to leave comments, you need to log in
Why does Model.objects.create() throw a duplicate key value violates unique constraint error?
The error is thrown at the time of saving the object, the problem is that it Model.objects.create()
gives the new object the id of an already existing one, checked with a debugger. Why does it do this and how can I make it generate a new one? Until recently, everything was fine and id was generated as it should, but suddenly in one place it stopped working.
Here is the traceback:
Environment:
Request Method: POST
Request URL: http://192.168.10.222:8080/base/add_merop_from_template/1/
Django Version: 2.2
Python Version: 3.8.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'templatesApp.apps.TemplatesappConfig',
'changelog.apps.ChangelogConfig',
'accounts.apps.AccountsConfig',
'dashboard.apps.DashboardConfig',
'reference.apps.ReferenceConfig',
'baseTables.apps.BasetablesConfig',
'adminTables.apps.AdmintablesConfig',
'reps.apps.RepsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in _execute
84. return self.cursor.execute(sql, params)
The above exception (duplicate key value violates unique constraint "baseTables_merop_pkey"
DETAIL: Key (id)=(19) already exists.
) was the direct cause of the following exception:
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/home/itpw/project/nik/vkmtsup_django/vkmt_sup/baseTables/views.py" in AddMeropFromTemplate
1748. m = Merop.objects.create()
File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py" in create
422. obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py" in save
740. self.save_base(using=using, force_insert=force_insert,
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py" in save_base
777. updated = self._save_table(
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py" in _do_insert
907. return manager._insert([self], fields=fields, return_id=update_pk,
File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py" in execute_sql
1332. cursor.execute(sql, params)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in execute
99. return super().execute(sql, params)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in execute
67. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in _execute_with_wrappers
76. return executor(sql, params, many, context)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in _execute
84. return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.8/dist-packages/django/db/utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py" in _execute
84. return self.cursor.execute(sql, params)
Exception Type: IntegrityError at /base/add_merop_from_template/1/
Exception Value: duplicate key value violates unique constraint "baseTables_merop_pkey"
DETAIL: Key (id)=(19) already exists.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question