question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integrity Error: update or delete on table 'cms_cmsplugin' violates foreign key constraint

See original GitHub issue

I’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:

  1. Add a new Project Collection plugin to a page in the admin.
  2. Select which ProjectCollection to display in the plugin from the dropdown menu.
  3. Click save and publish the page.
  4. 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:closed
  • Created 10 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
glic3rinucommented, Apr 3, 2015

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.

1reaction
ghostcommented, Dec 13, 2019

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 add FooCMSPluginModel to myapp.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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Integrity Error: update or delete on table 'cms_cmsplugin' ...
connection.commit() IntegrityError: update or delete on table "cms_cmsplugin" violates foreign key constraint "cmsplugin_ptr_id_refs_id_6702e4c7 ...
Read more >
update or delete on table "users_user" violates foreign key ...
You can't reference not existing PK in most of DBs. If you want to leave Article after deleting User you should do something...
Read more >
update or delete on table 'cms_cmsplugin' violates foreign ...
It would appear that the one-to-one relation between CMSPlugin and my ProjectCollectionCMSPluginModel is causing a foreign key constraint error when attempting ...
Read more >
Integrityerror At /Admin/Auth/User/11/Delete/ Foreign Key ...
I'm trying to delete a user from my admin panel. The system tells me that it causes an integrity error by violating a...
Read more >
Django : IntegrityError Insert or update on table "orders_order ...
Django : IntegrityError Insert or update on table "orders_order" violates foreign key constraint " [ Beautify Your Computer ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found