Opened 10 years ago

Closed 8 years ago

#23776 closed Bug (worksforme)

HStoreField example doesn't work

Reported by: vedgar Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords: postgres hstore register
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

From https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#querying-hstorefield

With given model (Dog), in shell,

>>> from atest.models import Dog
>>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})

gives "can't adapt type 'dict'" error. After researching, it seems to me it's because noone called psycopg2.extras.register_hstore, that must be called to enable storing dict as hstore in a Postgres database. (If django_hstore is in the list of INSTALLED_APPS, it will call register_hstore by itself, which is probably why it works for some people. But once you remove django_hstore from INSTALLED_APPS, you'll see the bug. Since django_hstore is external app, Django probably shouldn't rely on it.)

I'm not quite sure how to fix it, since I don't know who should be responsible for registering hstore... there is a HStoreExtension operation that does it when migrating, but I have a feeling it should be inserted automatically from makemigrations (when app uses hstore), not shift the burden on users. Even if users are expected to register hstore manually, it should be clearly stated in the docs.

Change History (8)

comment:1 by Tim Graham, 10 years ago

Did you add 'django.contrib.postgres' to INSTALLED_APPS? That should set things up I believe.

comment:2 by vedgar, 10 years ago

Yes, that's one of first things I did. It didn't help.

comment:3 by Florian Apolloner, 10 years ago

comment:4 by vedgar, 10 years ago

Hm, I changed pass to raise and it worked. Since that didn't make any sense, I changed it back and it still works. I updated django to latest before that, so maybe it was fixed. But it's possible that I made a mistake earlier - sorry for bothering you.

(Just out of curiosity: when makemigrations says "No changes detected", or migrate says "No migrations to apply", is it still possible that something happened to my app? For instance, hstore was registered or something like that.:)

comment:5 by Tim Graham, 10 years ago

Resolution: worksforme
Status: newclosed

comment:6 by Sassan Haradji, 8 years ago

I had to run CREATE EXTENSION hstoredict in Postgres shell. Before that it raised the above error even though I had 'django.contrib.postgres' in INSTALLED_APPS.

Version 0, edited 8 years ago by Sassan Haradji (next)

comment:7 by Sassan Haradji, 8 years ago

Resolution: worksforme
Status: closednew

comment:8 by Tim Graham, 8 years ago

Component: Database layer (models, ORM)Documentation
Resolution: worksforme
Status: newclosed

That step is documented, "Setup the hstore extension in PostgreSQL before the first CreateModel or AddField operation by adding a migration with the HStoreExtension operation. "

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