[Django] #27846: refresh_from_db() does not create "soft links" on model_instances that are pointed to by another model's OneToOneField

30 views
Skip to first unread message

Django

unread,
Feb 15, 2017, 3:14:52 PM2/15/17
to django-...@googlegroups.com
#27846: refresh_from_db() does not create "soft links" on model_instances that are
pointed to by another model's OneToOneField
-------------------------------------+-------------------------------------
Reporter: Keith | Owner: nobody
Hostetler |
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.10
layer (models, ORM) | Keywords: refresh_from_db
Severity: Normal | OneToOneField
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Sorry for the poor summary, it is difficult to explain in words. I have a
project to demo this bug attached to this ticket, but I will try to
explain the bug anyway in steps and the setup.

Setup:
- 2 models (A and B)
- B has a OneToOne to A
- Both A and B have a field (ie TextField)
- Setup either a signal or override save() for A to update B's TextField
to equal that of A's on save() or post_save for signals

Steps:
- Create instance of A
- Get another copy of the instance of A via A.objects.get()
- Create instance of B using the copy of the instance of A
- Do refresh_from_db() on original instance of A
- Try to access B from A

The project I have provided is a slim version of this problem that
demonstrates it with signals, overriden save(), and basic set and save
inside the test. The basic set and save works, but the other two fail when
using the above steps.

--
Ticket URL: <https://code.djangoproject.com/ticket/27846>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 15, 2017, 3:15:28 PM2/15/17
to django-...@googlegroups.com
#27846: refresh_from_db() does not create "soft links" on model_instances that are
pointed to by another model's OneToOneField
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage:
OneToOneField | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Keith Hostetler):

* Attachment "django_1_10_refresh_from_db_bug.rar" added.

Django

unread,
Feb 15, 2017, 4:22:18 PM2/15/17
to django-...@googlegroups.com
#27846: refresh_from_db() does not create "soft links" on model_instances that are
pointed to by another model's OneToOneField
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage:
OneToOneField | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Keith Hostetler):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:1>

Django

unread,
Feb 15, 2017, 5:55:16 PM2/15/17
to django-...@googlegroups.com
#27846: refresh_from_db() does not create "soft links" on model_instances that are
pointed to by another model's OneToOneField
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage:
OneToOneField | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Keith Hostetler:

Old description:

> Sorry for the poor summary, it is difficult to explain in words. I have a
> project to demo this bug attached to this ticket, but I will try to
> explain the bug anyway in steps and the setup.
>
> Setup:
> - 2 models (A and B)
> - B has a OneToOne to A
> - Both A and B have a field (ie TextField)
> - Setup either a signal or override save() for A to update B's TextField
> to equal that of A's on save() or post_save for signals
>
> Steps:
> - Create instance of A
> - Get another copy of the instance of A via A.objects.get()
> - Create instance of B using the copy of the instance of A
> - Do refresh_from_db() on original instance of A
> - Try to access B from A
>
> The project I have provided is a slim version of this problem that
> demonstrates it with signals, overriden save(), and basic set and save
> inside the test. The basic set and save works, but the other two fail
> when using the above steps.

New description:

Sorry for the poor summary, it is difficult to explain in words. I have a
project to demo this bug attached to this ticket, but I will try to
explain the bug anyway in steps and the setup.

Setup:
- 2 models (A and B)
- B has a OneToOne to A
- Both A and B have a field (ie TextField)
- Setup either a signal or override save() for A to update B's TextField
to equal that of A's on save() or post_save for signals

Steps:
- Create instance of A
- Get another copy of the instance of A via A.objects.get()
- Create instance of B using the copy of the instance of A
- Do refresh_from_db() on original instance of A
- Try to access B from A

The project I have provided is a slim version of this problem that
demonstrates it with signals, overriden save(), and basic set and save
inside the test. The basic set and save works, but the other two fail when

using the above steps. Run the test suite to see.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:2>

Django

unread,
Feb 15, 2017, 7:57:14 PM2/15/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields

-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

I think this is fixable but I'm not sure without trying to write a patch.
This case may be discussed in ticket:901#comment:36 It looks like the
issue is that `refresh_from_db()`
[https://github.com/django/django/blob/a7214f0e84913a27e0b73de89d4c827ef1c53b94/django/db/models/base.py#L609-L620
clears concrete fields] but doesn't clear reverse relations.

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:3>

Django

unread,
Apr 5, 2017, 8:23:12 PM4/5/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Matvey
| Kukuy
Type: Bug | Status: assigned

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matvey Kukuy):

* owner: nobody => Matvey Kukuy
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:4>

Django

unread,
May 29, 2017, 6:11:22 PM5/29/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matvey Kukuy):

* status: assigned => new
* owner: Matvey Kukuy => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:5>

Django

unread,
Jun 6, 2017, 7:42:29 PM6/6/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Paulo):

* owner: nobody => Paulo


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:4>

Django

unread,
Jul 19, 2017, 5:48:47 PM7/19/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrey Fedoseev):

* cc: Andrey Fedoseev (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:5>

Django

unread,
Aug 24, 2017, 6:24:18 PM8/24/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Paulo):

Hello,
What do you all think about clearing the fields cache entirely on
refresh_from_db()?
This would cover similar bugs where a many to one or many to many
relationship is cached.
The only case we can't cover is fields prefetched to custom attributes.

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:6>

Django

unread,
Sep 10, 2017, 5:48:58 PM9/10/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Berker Peksag):

* cc: Berker Peksag (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:7>

Django

unread,
Sep 19, 2017, 1:56:00 PM9/19/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Paulo):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/9112

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:8>

Django

unread,
Oct 12, 2017, 5:25:25 PM10/12/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"a7b5ad8b19a08d7d57302ece74f6e26d2887fd9f" a7b5ad8b]:
{{{
#!CommitTicketReference repository=""
revision="a7b5ad8b19a08d7d57302ece74f6e26d2887fd9f"
Fixed #27846 -- Made Model.refresh_from_db() clear cached relations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:9>

Django

unread,
Oct 12, 2017, 5:42:33 PM10/12/17
to django-...@googlegroups.com
#27846: refresh_from_db() doesn't clear reverse OneToOneFields
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: refresh_from_db | Triage Stage: Accepted
OneToOneField |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"5d3f2aa3f12747822fb1ece95f0f614eeafa47b1" 5d3f2aa]:
{{{
#!CommitTicketReference repository=""
revision="5d3f2aa3f12747822fb1ece95f0f614eeafa47b1"
[2.0.x] Fixed #27846 -- Made Model.refresh_from_db() clear cached
relations.

Backport of a7b5ad8b19a08d7d57302ece74f6e26d2887fd9f from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27846#comment:10>

Reply all
Reply to author
Forward
0 new messages