Key (extended_object_id) already exists.

Jan. 5, 2019, 9:28 a.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:

  1. Create an extension (or "Save" in another way from Menu / Page / My extension)
  2. Publish page
  3. Go to the draft version by clicking "Edit"
  4. Remove extension (by going to Menu / Page / My extension and clicking "Delete")
  5. Create an extension again (Menu / Page / My Extension)
  6. 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.

Rate this article

0 from 5 (total 0 ratings)

You can send feedback, suggestions or comments on this article using this form:

Fields marked by star ( * ) is required.

Thank you for yor feedback!

After clicking the "Send" button, your message will be delivered to me on the mail.

Author of Article

The right to use content on this page https://vivazzi.pro/en/it/extended-object-id-already-exists/:

Permission is granted to copy an content with its author and reference to the original without using the parameter rel="nofollow" in tag <a>. Usage:

Author of Article: Maltsev Artem
Link to article: <a href="https://vivazzi.pro/en/it/extended-object-id-already-exists/">https://vivazzi.pro/en/it/extended-object-id-already-exists/</a>

More: Terms of site usage

Comments: 0

You can leave a comment as an unregistered user.

But if you sing up, you can:

  • receive notifications
  • view your comments
  • be able to use all the functions of the developed services

To comment in one's own name you should log in or sign up in the usual way or through social networks:

Send

There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it

Select currency for displaying monetary values