{"action":"opened","issue":{"url":"https://api.github.com/repos/treyhunner/django-simple-history/issues/185","labels_url":"https://api.github.com/repos/treyhunner/django-simple-history/issues/185/labels{/name}","comments_url":"https://api.github.com/repos/treyhunner/django-simple-history/issues/185/comments","events_url":"https://api.github.com/repos/treyhunner/django-simple-history/issues/185/events","html_url":"https://github.com/treyhunner/django-simple-history/issues/185","id":94820225,"number":185,"title":"Error when performing a cascading delete on models with foreign keys","user":{"login":"njones11","id":459951,"avatar_url":"https://avatars.githubusercontent.com/u/459951?v=3","gravatar_id":"","url":"https://api.github.com/users/njones11","html_url":"https://github.com/njones11","followers_url":"https://api.github.com/users/njones11/followers","following_url":"https://api.github.com/users/njones11/following{/other_user}","gists_url":"https://api.github.com/users/njones11/gists{/gist_id}","starred_url":"https://api.github.com/users/njones11/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/njones11/subscriptions","organizations_url":"https://api.github.com/users/njones11/orgs","repos_url":"https://api.github.com/users/njones11/repos","events_url":"https://api.github.com/users/njones11/events{/privacy}","received_events_url":"https://api.github.com/users/njones11/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-13T21:49:40Z","updated_at":"2015-07-13T21:49:40Z","closed_at":null,"body":"I'm using django simple history in combination with the Django CMS Mezzanine, and in that situation we have curriculum vitaes (CVs) for our users where each CV is a mezzanine page. Additionally, there are entries which appear on these CVs (publications, awards, etc.) which are models with foreign keys to these CV pages. These CV items (publications, awards, etc) have their history tracked with simple history.\r\n\r\nWhen deleting a CV page, the CV items are also deleted through a cascade, which is desired. However, when doing this cascading delete, Django throws a database integrity error, since the simple history entries don't get deleted and they now reference nonexistent models (the CV page). \r\n\r\nI understand why simple history doesn't delete historical models when the base model is deleted, but it seems like its handling of foreign keys isn't correct. I can work around the issue I'm encountering by either a) setting the foreign keys on the cv items to be nullable or by b) overriding the delete method on the CV page to also delete the historical entries before calling super().delete()\r\n\r\nHowever setting the keys to be nullable seems bad, and overriding the delete method on the CV page doesn't seem like it should be necessary. \r\n\r\nIt seems like the proper solution here would be for simple history to always create foreign keys in the historical model which are nullable, even when the foreign keys in the database are not nullable.  This way, a simple history object could reference a deleted item without violating database constraints. "}}