Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24751 closed Bug (fixed)

hstore isnull lookup fails

Reported by: mrAdm Owned by: Tim Graham <timograham@…>
Component: contrib.postgres Version: 1.8
Severity: Release blocker Keywords: hstore
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


from django.db import models
from django.contrib.postgres.fields import HStoreField

class HStoreModel(Model):
    hstore_field = HStoreField()

Execute the query:

HStoreModel.objects.create(hstore_field={'a': None})
# or non existing key


Traceback (most recent call last):
  File "/vagrant/", line 10, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 338, in execute_from_command_line
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 312, in execute
  File "/usr/local/lib/python2.7/dist-packages/django/", line 18, in setup
  File "/usr/local/lib/python2.7/dist-packages/django/apps/", line 115, in populate
  File "/vagrant/st/", line 12, in ready
    print HStoreModel.objects.filter(hstore_field__a__isnull=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 162, in __iter__
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 238, in iterator
    results = compiler.execute_sql()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 829, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: operator does not exist: hstore -> boolean
LINE 1: ...storemodel" WHERE "st_hstoremodel"."hstore_field" -> 'a' IS ...
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Wrong SQL syntax?:

WHERE "st_hstoremodel"."hstore_field" -> 'b' IS NULL

Maybe you need this:

WHERE ("st_hstoremodel"."hstore_field" -> 'b') IS NULL

Attachments (1)

24751-test.diff (692 bytes ) - added by Tim Graham 9 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

Attaching a failing test.

by Tim Graham, 9 years ago

Attachment: 24751-test.diff added

comment:2 by Josh Smeaton, 9 years ago

Is there any reason these hstore lookups couldn't always be wrapped in brackets (assuming this fixes the isnull problem)? We do a similar thing with F-combinables already:

comment:3 by Anssi Kääriäinen, 9 years ago

+1 to brackets. It should fix the isnull problem.

comment:4 by Marc Tamlyn, 9 years ago

Can't think of a reason the brackets would be a problem. We likely will need the same thing for JSON as well.

comment:5 by Tim Graham, 9 years ago

Has patch: set

comment:6 by Tim Graham <timograham@…>, 9 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 3c8fe5d:

Fixed #24751 -- Fixed HStoreField isnull lookup.

comment:7 by Tim Graham <timograham@…>, 9 years ago

In df0674ea:

[1.8.x] Fixed #24751 -- Fixed HStoreField isnull lookup.

Backport of 3c8fe5dddf34533a419d2deed5208a28de32cb4a from master

Note: See TracTickets for help on using tickets.
Back to Top