2

I have a django project currently hosting multiple sites, and now I need to split them into different projects with different databases.

So I cloned the project and thinking delete the records in Site model will cascade delete all data related to the sites I am deleting and leaving the ones that relates to the site record I am keeping.

I have the following code in management command to delete the sites:

Site.objects.filter(~Q(domain=options.get("domain"))).delete()

But I am getting error like this:

django.db.utils.IntegrityError: update or delete on table "django_site" violates foreign key constraint "core_staticpage_site_id_fkey" on table "core_staticpage"
DETAIL:  Key (id)=(4) is still referenced from table "core_staticpage".

This error applies to ForeignKey and ManyToManyField, Django version is 1.4.3

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
James Lin
  • 25,028
  • 36
  • 133
  • 233
  • Sorry what do you mean by that? – James Lin Feb 27 '13 at 02:26
  • But isn't what I have described in the question? Related data in model StaticPage should be cascade deleted with related record in Site model is deleted. – James Lin Feb 27 '13 at 02:34
  • yeah I know, I'm just telling you another solution. Why not rewrite your codes, first evaluate the static page with the id of the site and delete it before you delete the main data on the site. It's your option... – catherine Feb 27 '13 at 02:44
  • well, yeah that's my current work around, but I would wonder why it doesn't cascade delete though. Thanks for trying to help. – James Lin Feb 27 '13 at 02:50
  • I don't have knowledge about that but I will try to figure it out why so that I will learn something new :) and I will post the reason here – catherine Feb 27 '13 at 02:51
  • maybe this will help you: http://stackoverflow.com/questions/2475249/what-are-the-options-for-overriding-djangos-cascading-delete-behaviour – catherine Feb 27 '13 at 02:55
  • @catherine I don't think the project has anywhere overwriting the on_delete method – James Lin Feb 27 '13 at 02:59
  • If `core.StaticPage` is yours, try setting the option `on_delete` on the ForeignKey to Sites, anything other than `models.PROTECT` should work, but that depends on what you want to hapen when you delete a Site. – Lorenzo Peña Jul 03 '15 at 18:58

0 Answers0