[Django] #24418: refresh_from_db is breaking on null foreign keys

16 views
Skip to first unread message

Django

unread,
Feb 26, 2015, 9:19:02 AM2/26/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
----------------------------------------------+-----------------------
Reporter: ppp23 | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.8alpha1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+-----------------------
If i try to call refresh_from_db on a model instance that has a foreignkey
set to null, i get an error:

{{{
Traceback:
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request,
*callback_args, **callback_kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/contrib/admin/options.py" in wrapper
465. return self.admin_site.admin_view(view)(*args,
**kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/db/transaction.py" in inner
371. return func(*args, **kwargs)
File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
packages/django/contrib/admin/options.py" in add_view
1164. self.save_model(request, new_object, form, False)
File "/home/johannes/formonauts/donation/server/django-
donut/donut/admin.py" in save_model
36. obj.save()
File "/home/johannes/formonauts/donation/server/django-
donut/nanny/models.py" in save
109. self.refresh_from_db()
File "/home/johannes/formonauts/donation/server/django-
donut/donut/basemodels.py" in refresh_from_db
80. related_val = getattr(rel_instance,
field.related_field.attname)

Exception Type: AttributeError at /admin/nanny/team/add/
Exception Value: 'NoneType' object has no attribute 'id'
}}}

here rel_instance is assigned:
https://github.com/django/django/blob/stable/1.8.x/django/db/models/base.py#L630

if it is None (foreignkey not set, as it is nullable in this model) the
getattr function will complain that it can not get 'id' from 'NoneType'

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

Django

unread,
Feb 26, 2015, 12:25:25 PM2/26/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* severity: Normal => Release blocker
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

Reproduced with the attached test.

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

Django

unread,
Feb 27, 2015, 12:27:15 PM2/27/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* has_patch: 0 => 1


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

Django

unread,
Feb 27, 2015, 12:55:04 PM2/27/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* stage: Accepted => Ready for checkin


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

Django

unread,
Feb 27, 2015, 2:24:29 PM2/27/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz <claude@…>):

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


Comment:

In [changeset:"5cf96b49e43daea6d4a0ba1c80c45e90c74f4e47"]:
{{{
#!CommitTicketReference repository=""
revision="5cf96b49e43daea6d4a0ba1c80c45e90c74f4e47"
Fixed #24418 -- Prevented crash in refresh_from_db with null fk

Thanks Johannes Lerch for the report, Tim Graham for the test case,
and Simon Charette for the review.
}}}

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

Django

unread,
Feb 27, 2015, 2:25:52 PM2/27/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"b825ec38c1e16a271c69694562963eadbe0c6352"]:
{{{
#!CommitTicketReference repository=""
revision="b825ec38c1e16a271c69694562963eadbe0c6352"
[1.8.x] Fixed #24418 -- Prevented crash in refresh_from_db with null fk

Thanks Johannes Lerch for the report, Tim Graham for the test case,
and Simon Charette for the review.

Backport of 5cf96b49e4 from master.
}}}

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

Django

unread,
Mar 9, 2015, 12:28:40 PM3/9/15
to django-...@googlegroups.com
#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
Reporter: ppp23 | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by ppp23:

Old description:

New description:

If i try to call refresh_from_db on a model instance that has a foreignkey
set to null, i get an error:

(stacktrace was removed after ticket was resolved)

if it is None (foreignkey not set, as it is nullable in this model) the
getattr function will complain that it can not get 'id' from 'NoneType'

--

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

Reply all
Reply to author
Forward
0 new messages