Opened 6 years ago
Closed 6 years ago
#30281 closed Bug (wontfix)
Model.refresh_from_db() does not refresh related objects
Reported by: | Aleksander Zamojski | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.0 |
Severity: | Normal | Keywords: | refresh_from_db |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
After testing my application with Django 2.0.13 I noticed few of my test started failing (they were working on Django 2.1). After further investigation, I figured out the problem was coming from refresh_from_db method.
Here is a test that show the problem:
def test_error_on_refresh_from_db(self): def add_one(id): task_model = FakeTask.objects.get(id=id) task_model.task_group.finished_tasks_number = F('finished_tasks_number') + 1 task_model.task_group.save() task_model.task_group.refresh_from_db() task_model.save() self.assertEqual(task_model.task_group.finished_tasks_number, 1) task_group_model = FakeTaskGroup.objects.create() task_model = FakeTask.objects.create(task_group=task_group_model) add_one(task_model.id) task_model.refresh_from_db() self.assertEqual(task_model.task_group.finished_tasks_number, 1)
Here you can find the definition (light version) of the this models:
class FakeTaskGroup(models.Model): finished_tasks_number = models.PositiveIntegerField(default=0) class FakeTask(models.Model): task_group = models.ForeignKey(FakeTaskGroup, on_delete=models.CASCADE)
Note:
See TracTickets
for help on using tickets.
Django 2.0 is end-of-life April 2019 and only receives security and data loss fixes until then (but most likely there won't be another release).