django-cms / djangocms-history

Provides undo/redo functionality for django CMS operations
https://www.django-cms.org
Other
28 stars 13 forks source link

Error when deleted pages #13

Closed jmrivas86 closed 5 years ago

jmrivas86 commented 7 years ago

Summary

Error when deleted pages which have a placeholder that is referenced in PlaceholderAction:

In [16]: Page.objects.all().delete()
IntegrityError: update or delete on table "cms_placeholder" violates foreign key constraint "djangocms_history_placeholder_id_46ec65d1_fk_cms_placeholder_id" on table "djangocms_history_placeholderaction"
DETAIL:  Key (id)=(16410) is still referenced from table "djangocms_history_placeholderaction".
The above exception was the direct cause of the following exception:

IntegrityError                            Traceback (most recent call last)
<ipython-input-16-3eceeb24531f> in <module>()
----> 1 Page.objects.all().delete()

~/Envs/example-cms/lib/python3.6/site-packages/treebeard/mp_tree.py in delete(self)
    124         if toremove:
    125             qset = get_result_class(self.model).objects.filter(reduce(operator.or_, toremove))
--> 126             super(MP_NodeQuerySet, qset).delete()
    127 
    128 

~/Envs/example-cms/lib/python3.6/site-packages/django/db/models/query.py in delete(self)
    607         collector = Collector(using=del_query.db)
    608         collector.collect(del_query)
--> 609         deleted, _rows_count = collector.delete()
    610 
    611         # Clear the result cache, in case this QuerySet gets reused.

~/Envs/example-cms/lib/python3.6/site-packages/django/db/models/deletion.py in delete(self)
    310                     for obj in instances:
    311                         signals.post_delete.send(
--> 312                             sender=model, instance=obj, using=self.using
    313                         )
    314 

~/Envs/example-cms/lib/python3.6/site-packages/django/db/transaction.py in __exit__(self, exc_type, exc_value, traceback)
    221                     # Commit transaction
    222                     try:
--> 223                         connection.commit()
    224                     except DatabaseError:
    225                         try:

~/Envs/example-cms/lib/python3.6/site-packages/django/db/backends/base/base.py in commit(self)
    240         self.validate_thread_sharing()
    241         self.validate_no_atomic_block()
--> 242         self._commit()
    243         # A successful commit means that the database connection works.
    244         self.errors_occurred = False

~/Envs/example-cms/lib/python3.6/site-packages/django/db/backends/base/base.py in _commit(self)
    209         if self.connection is not None:
    210             with self.wrap_database_errors:
--> 211                 return self.connection.commit()
    212 
    213     def _rollback(self):

~/Envs/example-cms/lib/python3.6/site-packages/django/db/utils.py in __exit__(self, exc_type, exc_value, traceback)
     92                 if dj_exc_type not in (DataError, IntegrityError):
     93                     self.wrapper.errors_occurred = True
---> 94                 six.reraise(dj_exc_type, dj_exc_value, traceback)
     95 
     96     def __call__(self, func):

~/Envs/example-cms/lib/python3.6/site-packages/django/utils/six.py in reraise(tp, value, tb)
    683             value = tp()
    684         if value.__traceback__ is not tb:
--> 685             raise value.with_traceback(tb)
    686         raise value
    687 

~/Envs/example-cms/lib/python3.6/site-packages/django/db/backends/base/base.py in _commit(self)
    209         if self.connection is not None:
    210             with self.wrap_database_errors:
--> 211                 return self.connection.commit()
    212 
    213     def _rollback(self):

IntegrityError: update or delete on table "cms_placeholder" violates foreign key constraint "djangocms_history_placeholder_id_46ec65d1_fk_cms_placeholder_id" on table "djangocms_history_placeholderaction"
`DETAIL:  Key (id)=(16410) is still referenced from table "djangocms_history_placeholderaction".
`

Expected behaviour

The page can be deleted with all its history

Actual behaviour

The page can not be deleted because it have a placeholder linked to djangocms_history

Environment

czpython commented 7 years ago

Hello @jmrivas86, This problem happens when djangocms_history is not in your INSTALLED_APPS. Because of this, django's orm can't cascade the deletion so the database raises an exception. I don't think there's much we can do here since Django allows you to add/remove apps from your INSTALLED_APPS without a clean up action. I'd have to check further.

jmrivas86 commented 7 years ago

Hi @czpython I have djangocms_history in my INSTALLED_APPS and running the migrations... maybe is the order?

czpython commented 7 years ago

Hello @jmrivas86, If you edit a plugin inside a page, then undo/redo the change. Are you able to delete this page individually from the admin?