Opened 4 years ago

Closed 2 years ago

#23776 closed Bug (worksforme)

HStoreField example doesn't work

Reported by: vedgar Owned by: nobody
Component: Documentation Version: master
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



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 Changed 4 years ago by Tim Graham

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

comment:2 Changed 4 years ago by vedgar

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

comment:3 Changed 4 years ago by Florian Apolloner

comment:4 Changed 4 years ago by vedgar

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 Changed 4 years ago by Tim Graham

Resolution: worksforme
Status: newclosed

comment:6 Changed 2 years ago by Sassan Haradji

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.
edit: Using Django 1.10

Last edited 2 years ago by Sassan Haradji (previous) (diff)

comment:7 Changed 2 years ago by Sassan Haradji

Resolution: worksforme
Status: closednew

comment:8 Changed 2 years ago by Tim Graham

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