django-simple-history icon indicating copy to clipboard operation
django-simple-history copied to clipboard

Error of generated historical objects when doing django's cascade deletion

Open lpetrus32 opened this issue 3 years ago • 2 comments

Hello,

I have a problem with django-simple-history when I use cascade deletion on objects.

I have several tables in my database that inherit from other tables, so I’ve set the deletion to cascade. The problem is that when I want to query Historical Objects generated by django-simple-history, I do have the one in the parent table, but for objects in the children table, that were automatically deleted by Django, I this error :

MyApp.models.parentTable_object.DoesNotExist: parentTable_object matching query does not exist.

If I understand the problem, django-simple-history tried to create a Historical object for my deleted object but failed because it needed the parent object it inherited and that no longer exists at this point.

I can’t do without cascading so I hope it’s possible because django-simple-history has met all my expectations so far.

Thanks you for your help

Environment (please complete the following information):

  • OS: Windows 10
  • Browser: Firefox
  • Django Simple History Version: 3.1.1
  • Django Version: 4.0.3
  • Database Version: django's integrated SqLite3

lpetrus32 avatar Aug 04 '22 10:08 lpetrus32

I have started hitting this problem, too.

  • OS: macOS Ventura
  • Browser: Firefox
  • Django Simple History Version: 3.3.0
  • Django Version: 4.1.7
  • Database Version: PostgreSQL 14.4

aetaylor avatar Mar 21 '23 12:03 aetaylor

Would you mind providing a minimal example? 🙂

I'm not able to reproduce the error on either version 3.1.1 nor 3.3.0 (nor the current master branch), using the following code based on what I understood from the description:

# Models:

class Parent(models.Model):
    history = HistoricalRecords()


class Child(models.Model):
    parent = models.ForeignKey(Parent, models.CASCADE)

    history = HistoricalRecords()


# Tests:

p = Parent.objects.create()
Child.objects.create(parent=p)
Child.objects.create(parent=p)
self.assertEqual(Parent.objects.count(), 1)
self.assertEqual(Child.objects.count(), 2)
self.assertEqual(Parent.history.count(), 1)
self.assertEqual(Child.history.count(), 2)

p.delete()
self.assertEqual(Parent.objects.count(), 0)
self.assertEqual(Child.objects.count(), 0)
self.assertEqual(Parent.history.count(), 2)
self.assertEqual(Child.history.count(), 4)

ddabble avatar Sep 27 '23 16:09 ddabble