#28406 closed Bug (invalid)
bug with HStore that has a default value of empty dict - {}
Reported by: | Eyal Elkevity | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | hstore |
Cc: | Craig de Stigter | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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...
Change History (4)
comment:1 by , 7 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Type: | Uncategorized → Bug |
comment:2 by , 7 years ago
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.
comment:3 by , 7 years ago
Cc: | added |
---|
As documented:
In other words, you should use
default=dict
.