﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31435	RecursionError when deleting a model with one to many relationship.	Fabian Allendorf	nobody	"This issue appeared with upgrade from Django 2 to Django 3.0. I double checked and it definitely works with version <3.0.

There is a model with a duration field and the ""__init__"" method is overridden to save ""old"" values of this duration field.

{{{
class TimeSeries(models.Model):
  frequency = models.DurationField()
  def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._old_frequency = self.frequency
}}}

Another model extends this ""TimeSeries"" model and has a foreign key reference to the model which I tried to delete.

{{{
class RefModel(TimeSeries):
  main_model = models.ForeignKey(
    MainModel, on_delete=models.CASCADE, related_name=""refs""
  )

class MainModel(models.Model):
  pass
}}}

So I created an instance of ""MainModel"" ""main_a"" and added some ""RefModel"" instances with relationhip to ""main_a"".
When I call ""main_a.delete()"" it ends up in a stack overflow with the following stacktrace:

{{{
Traceback (most recent call last):
  File ""<input>"", line 1, in <module>
  File ""/lib/python3.8/site-packages/django/db/models/base.py"", line 937, in delete
    collector.collect([self], keep_parents=keep_parents)
  File ""/lib/python3.8/site-packages/django/db/models/deletion.py"", line 244, in collect
    if sub_objs:
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 280, in __bool__
    self._fetch_all()
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 75, in __iter__
    obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
  File ""/lib/python3.8/site-packages/django/db/models/base.py"", line 512, in from_db
    new = cls(*values)
  File ""/lib/python3.8/site-packages/django_timeseries/models.py"", line 27, in __init__
    self._old_frequency = self.frequency
  File ""/webserver/business_rules/thread_safe_model_patcher.py"", line 51, in _patched_get_attribute
    return Model.get(model_self, item)
  File ""/lib/python3.8/site-packages/django/db/models/query_utils.py"", line 139, in __get__
    instance.refresh_from_db(fields=[field_name])
  File ""/lib/python3.8/site-packages/django/db/models/base.py"", line 627, in refresh_from_db
    db_instance = db_instance_qs.get()
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 411, in get
    num = len(clone)
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 258, in __len__
    self._fetch_all()
  File ""/lib/python3.8/site-packages/django/db/models/query.py"", line 1261, in _fetch_all
<--- Stacktrace repeats itself from here on --->
}}}

""django_timeseries"" is a custom package which defines the ""TimeSeries"" class.
""thread_safe_model_patcher.py"" monkeypatches the ""Model"" class. It has no effect on whether the error happens. I removed the this patch and it still errors.

It seems to me that deleting ""main_a"" deletes all ""RefModel"" instances related to it (like it should) but while doing this, it gets stuck initializing the super class ""TimeSeries"" because the ""__init__"" accesses the ""frequency"" field."	Bug	closed	Database layer (models, ORM)	3.0	Normal	invalid	delete model defer recursionerror		Unreviewed	0	0	0	0	0	0
