[Django] #28406: bug with HStore that has a default value of empty dict - {}

6 views
Skip to first unread message

Django

unread,
Jul 17, 2017, 4:33:27 PM7/17/17
to django-...@googlegroups.com
#28406: bug with HStore that has a default value of empty dict - {}
-------------------------------------+-------------------------------------
Reporter: eyaleee | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords: hstore
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
using django version 1.11.2 I have in one of my models an HStoreField.
I've defined this field with "default={}" in the model definition, so when
creating a new object (using objects.create) - the new hstore field will
be an empty dictionary.

however - there is a weird bug, If I create a first object, it is created
with an empty dict indeed. but when creating another object, instead of an
empty dictionary - it is loaded with the dictionary of the previous, non-
related object!!. I'm not sure where's the problem in django's code but
maybe it's related to models/base.py, in line 518 there's a "if kwargs"
which returns false in the case of the empty dict that is my default.
maybe it should return something?

my workaround was simple of course - stop using the default value. just
wanted to let you know...

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

Django

unread,
Jul 17, 2017, 9:32:15 PM7/17/17
to django-...@googlegroups.com
#28406: bug with HStore that has a default value of empty dict - {}
-------------------------------------+-------------------------------------
Reporter: Eyal Elkevity | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: hstore | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => invalid
* type: Uncategorized => Bug


Comment:

[https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.default
As documented]:
The default can’t be a mutable object (model instance, list, set, etc.),
as a reference to the same instance of that object would be used as the
default value in all new model instances. Instead, wrap the desired
default in a callable.

In other words, you should use `default=dict`.

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

Django

unread,
Apr 2, 2018, 9:04:20 PM4/2/18
to django-...@googlegroups.com
#28406: bug with HStore that has a default value of empty dict - {}
-------------------------------------+-------------------------------------
Reporter: Eyal Elkevity | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: hstore | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Craig de Stigter):

I just got bit by this and it took several hours to track down what was
wrong. Some thoughts:

* Seems the docs should say "shouldn't" here rather than "can't", since
HStoreField happily accepts a mutable default value.
* There should be a load-time check for this. It's an easy mistake to
make, and very confusing / hard to track down.
* In fact, why doesn't the field reject `default={}` outright? Not every
mutable value is obvious, but dict instances are probably 99% of them, so
it could check for those.

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

Django

unread,
Apr 2, 2018, 9:06:23 PM4/2/18
to django-...@googlegroups.com
#28406: bug with HStore that has a default value of empty dict - {}
-------------------------------------+-------------------------------------
Reporter: Eyal Elkevity | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: hstore | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: Craig de Stigter (added)


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

Django

unread,
Apr 3, 2018, 9:32:55 AM4/3/18
to django-...@googlegroups.com
#28406: bug with HStore that has a default value of empty dict - {}
-------------------------------------+-------------------------------------
Reporter: Eyal Elkevity | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: hstore | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Tim Graham):

#28577 added checks for `JSONField` and `ArrayField` `default`. I added a
[https://github.com/django/django/pull/9852 PR] to do to the same for
`HStoreField`.

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

Reply all
Reply to author
Forward
0 new messages