Integrity Error: update or delete on table 'cms_cmsplugin' violates foreign key constraint
See original GitHub issueI’ve never run into this issue before so I was hesitant to post it here in case it’s user error on my part, but I cannot see what is causing this.
Here’s a rundown of my setup:
I have a Project Model. I then have a ProjectCollection model with a m2m relationship to Project.
I have a ProjectCollectionCMSPluginModel which subclasses CMSPlugin and has a fk relationship to ProjectCollection.
And finally I have a ProjectCollectionPlugin which subclasses CMSPluginBase with: model = ProjectCollectionCMSPluginModel
This setup lets me select a Project collection plugin in the admin and select which collection I want the plugin to display.
How the issue occurs:
- Add a new Project Collection plugin to a page in the admin.
- Select which ProjectCollection to display in the plugin from the dropdown menu.
- Click save and publish the page.
- Go back into the admin and attempt to delete the Project Collection plugin from the page.
The following error message is then given in the console and the plugin is not deleted:
Internal Server Error: /admin/cms/page/13/remove-plugin/
IntegrityError: update or delete on table "cms_cmsplugin" violates foreign key constraint "cmsplugin_ptr_id_refs_id_6702e4c7" on table "cmsplugin_projectcollectioncmspluginmodel"
DETAIL: Key (id)=(68) is still referenced from table "cmsplugin_projectcollectioncmspluginmodel".
It would appear that the one-to-one relation between CMSPlugin and my ProjectCollectionCMSPluginModel is causing a foreign key constraint error when attempting to delete the plugin.
The thing that I don’t understand is why this error would be occurring when that relationship setup is built into django. I don’t see what I could be doing wrong that would cause this error this time, when I’ve never encountered this error before when creating custom plugins. It seems that deleting the plugin should properly cascade delete.
Here’s my full code so you can see what’s going on:
Django==1.5.2 django-cms==2.4.2 mptt==0.5.2
PostgreSQL 9.1.4
models.py
class Project (models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1000)
...
class ProjectCollection (models.Model):
projects = models.ManyToManyField(Project, related_name='collection')
class ProjectCollectionCMSPluginModel (CMSPlugin):
collection = models.ForeignKey(ProjectCollection, blank=True, null=True, related_name='plugin_model')
cms_plugins.py
@plugin_pool.register_plugin
class ProjectCollectionPlugin (CMSPluginBase):
name = 'Project Collection'
model = ProjectCollectionCMSPluginModel
render_template = 'plugins/collections/project.html'
def render(self, context, instance, placeholder):
context['instance'] = instance
context['placeholder'] = placeholder
return context
I’ve been beating my head against the desk trying to figure this out for half a day and any help would be greatly appreciated.
Issue Analytics
- State:
- Created 10 years ago
- Comments:18 (6 by maintainers)
Top GitHub Comments
Stumbled here after googling for this error. My app produces a very similar error, not fully understanding why yet, but what I’ve seen is that the order in which Django ORM deletes related objects is non-deterministic, and I believe this explains why some times breaks and sometimes don’t.
As a reference for future users, I ran into this same error for a different reason.
CMS uses
models.query.QuerySet.delete(plugins)
to delete plugins on publish. If your models are in a sub-module of your apps models (eg.myapp.models.my_awesome_cms_models.FooCMSPluginModel
) and you forgot to addFooCMSPluginModel
tomyapp.models.__init__
, CMS will happily work with this as you registered the CMSPlugin explicitly.But once you try to publish a page with this plugin, django can’t find the actual model, won’t delete the
FooCMSPluginModel
instance and try to delete the CMSPlugin instance resulting in the error mentioned above.(django-cms == 3.7.1)