我试着用this nice tutorial来使用django-hstore。我在一个由South管理的现有应用中添加了两个类:
class Attribute(models.Model):
name = models.CharField(max_length=200, verbose_name=_("name"))
description = models.CharField(max_length=1000, verbose_name=_("description"))
class Measure(models.Model):
attribute = models.ForeignKey(Attribute)
data = hstore.DictionaryField(db_index=True)
objects = hstore.HStoreManager()创建了一个schemamigration --auto,启动了迁移,并获得了一个django.db.utils.DatabaseError: type "hstore" does not exist。
好了,tuto似乎不完整,django-hstore documentation告诉我使用自定义数据库后端,我在设置文件中添加了以下内容:
DATABASES['default']['ENGINE'] = 'django_hstore.postgresql_psycopg2'然后我在south/db/__init__.py", line 78得到了一个KeyError: 'default'。在这一点上,intertubes +一些试验/错误将我指向SOUTH_DATABASE_ADAPTERS设置变量,我在设置中添加了以下内容:
SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}新错误:
File ".../psycopg2/extras.py", line 769, in register_hstore
"hstore type not found in the database. "
psycopg2.ProgrammingError: hstore type not found in the database. please install it from your 'contrib/hstore.sql' file这很奇怪,因为我安装了hstore扩展:
$ sudo -u postgres psql
create extension hstore;
postgres=# CREATE EXTENSION hstore;
ERROR: extension "hstore" already exists
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+--------------------------------------------------
hstore | 1.0 | public | data type for storing sets of (key, value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
postgres=# SELECT 'hstore'::regtype::oid;
oid
-------
57704
(1 row)这是怎么回事呢?我使用的是Django 1.4,Postgresql 9.1。
发布于 2012-07-20 23:05:32
我最终发现我使用的特定数据库没有安装hstore扩展:
$ psql -d mydb
psql (9.1.4)
Type "help" for help.
mydb=# SELECT t.oid, typarray FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore';
oid | typarray
-----+----------
(0 rows)
mydb=# \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)
mydb=# create extension hstore;
WARNING: => is deprecated as an operator name
DETAIL: This name may be disallowed altogether in future versions of PostgreSQL.
CREATE EXTENSION
mydb=# \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+--------------------------------------------------
hstore | 1.0 | public | data type for storing sets of (key, value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
mydb=# SELECT t.oid, typarray FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore';
oid | typarray
-------+----------
58800 | 58805
(1 row)我以为在安装hstore之后创建的数据库会包含这个扩展名。似乎不是这样的,am I misinterpreting how extensions work ? Are they database-specific ?
发布于 2018-09-14 19:08:21
Django现在包含a migration operation to create the hstore extension in PostgreSQL
from django.contrib.postgres.operations import HStoreExtension
class Migration(migrations.Migration):
...
operations = [
HStoreExtension(),
...
]发布于 2014-05-13 16:00:08
自从我上一次回答以来,Django已经弃用并删除了pre_syncdb信号。我已经更新了答案以适应更新的版本。对于较新的版本,基本机制是相同的,因为这两种方法都依赖于信号和仅在HSTORE扩展不存在时才执行的SQL代码。
Django 1.8+
自从Django引入数据库迁移以来,pre_syncdb信号是和。然而,他们引入了一种名为pre_migrate的新信号,可以以同样的方式使用。
示例:
"""
This is an example models.py which contains all model definition.
"""
from django.db import connection, models
from django.db.models.signals import pre_migrate
from django.dispatch import receiver
import sys
# sender is optional but will be called for every pre_migrate signal if removed
@receiver(pre_migrate, sender=sys.modules[__name__])
def setup_postgres_hstore(sender, **kwargs):
"""
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database.
Requires PostgreSQL 9.1 or newer.
"""
cursor = connection.cursor()
cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore")
# ...rest of your model definition goes here
class Foo(models.Model):
# ...field definitions, etc.Django 1.6+ (原始答案)
确保在./manage.py syncdb期间安装HSTORE扩展的一种方法是在models.py文件中使用Django1.6引入的pre_syncdb信号。
示例:
"""
This is an example models.py which contains all model definition.
"""
from django.db import connection, models
from django.db.models.signals import pre_syncdb
from django.dispatch import receiver
import sys
# sender is optional but will be called for every pre_syncdb signal if removed
@receiver(pre_syncdb, sender=sys.modules[__name__])
def setup_postgres_hstore(sender, **kwargs):
"""
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database.
Requires PostgreSQL 9.1 or newer.
"""
cursor = connection.cursor()
cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore")
# ...rest of your model definition goes here
class Foo(models.Model):
# ...field definitions, etc.如果你不想为每个新的数据库实例运行它,我发现这是很有用的。在测试数据库设置期间,此方法也适用于Django单元测试。
关于Django中信号钩子的更多信息:https://docs.djangoproject.com/en/1.6/ref/signals/#management-signals
https://stackoverflow.com/questions/11577993
复制相似问题