djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

register_hstore_handler is not run during django management commands #141

Open GooseYArd opened 8 years ago

GooseYArd commented 8 years ago

Hi All,

I'm experiencing the same problem as issue #93, I'm not a postgis user though, and neither of the two suggested settings workarounds seem to affect my issue, which occurs when running a Django management command that uses a couple of models that contain hstore fields. I'm using Django 1.8.6, django_hstore 1.4.1 and psycopg2 2.6.1.

I've confirmed that the hstore oids are consistent among the databases, and then did a little printf debugging. I found that register_hstore_handler is never called, but that I can fix the str-instead-of-dict problem by explicitly calling psycopg2.extras.register_hstore during the management command's constructor.

We don't use the transaction middleware, but do have a number of other middlewares enabled that I presume could affect signal delivery in the same way.

I'd like to provide more useful information in the issue request, but at the moment I'm not sure what to check next- any advice appreciated.

sanchitsharma commented 8 years ago

I am too facing the same issue. Can you elaborate a bit on how you are solving the issue for now .i.e (where do you get the connection object to pass in the register_hstore function)

clifff commented 8 years ago

I ran into this problem too - in both manage.py shell and in a mangement command, I'd get ProgrammingError: can't adapt type 'HStoreDict' errors every time I tried to create a model instance with an Hstore field.

@sanchitsharma: I worked around it by adding this to my management command:

from django.db import connection
from psycopg2.extras import register_hstore
# And then in `def handle`
register_hstore(connection.cursor(), globally=True, unicode=True)
nemesifier commented 8 years ago

is django_hstore in your settings.INSTALLED_APPS?

chuck commented 7 years ago

I'm suddenly seeing this after upgrading from Django 1.7 to Django 1.8.17 (and from django-hstore 1.4 to 1.4.2, though not surprisingly reverting that minor upgrade doesn't change anything).

django_hstore is in settings.INSTALLED_APPS. Fiddling with the order of INSTALLED_APPS hasn't resulted in any improvement.

I don't have a small reproducible case, but I'm happy to debug. The workaround described by @clifff works for me, but would of course be pretty ugly to add to every management command.

DustinHolden commented 7 years ago

I just started using django_hstore with a project and am encountering this same issue, and the workaround worked for me. Just curious if anyone had any more information on this before I started digging into it?

Django 1.8.18, django_hstore 1.4.2.

chuck commented 7 years ago

@DustinHolden which workaround worked for you? I have one that works in shell/shell_plus, but I haven't figured out how to incorporate it into migration generation, which is a constant pain.