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 , 10 years ago
comment:3 by , 10 years ago
@vedgar: Can you try remplacing pass with raise in https://github.com/django/django/blob/master/django/contrib/postgres/signals.py#L16-L25 ?
comment:4 by , 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 , 10 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:6 by , 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
.
comment:7 by , 8 years ago
Resolution: | worksforme |
---|---|
Status: | closed → new |
comment:8 by , 8 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
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. "
Did you add
'django.contrib.postgres'
toINSTALLED_APPS
? That should set things up I believe.