Key (extended_object_id) already exists.
Jan. 5, 2019, 2:28 p.m.
When publishing a page in django-cms (3.5.0), an error may occur:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "spec_pageext_extended_object_id_key" DETAIL: Key (extended_object_id)=(324) already exists.
From treysbeka it is clear that the error is related to Extension
(PageExtension).
Still need to say that the error does not always appear when publishing the page. To reproduce the error you need:
- Create an extension (or "Save" in another way from Menu / Page / My extension)
- Publish page
- Go to the draft version by clicking "Edit"
- Remove extension (by going to Menu / Page / My extension and clicking "Delete")
- Create an extension again (Menu / Page / My Extension)
- And try to publish the page.
An error appears in the sixth step.
It turns out that when publishing, the old Extension
object is not erased and an exception of the key uniqueness arises. Therefore, I wrote Mixin
, which, before performing operations on publishing the Extension
, deletes the old Extension
from the page:
class ExtensionFixMixin(object): def copy_to_public(self, public_object, language): if not self.public_extension: try: self.__class__.objects.get(extended_object_id=public_object.pk).delete() except self.__class__.DoesNotExist: pass return super(ExtensionFixMixin, self).copy_to_public(public_object, language)
Now you can use in your extensions:
class MyExtension(ExtensionFixMixin, PageExtension): ...
There is a ticket on the django-cms package in github, where this error occurs. I added a comment to my ticket. My solution is a workaround. How exactly to fix django-cms itself I didn’t look much. I hope the developers will fix this bug.
Comments: 0