我试图使用django hstore使用this nice tutorial。我在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
。
好吧,图图似乎不完整,django hstoredocumentation告诉我使用自定义数据库后端,我在设置文件中添加了以下内容:
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。
自从我的最后一个答案以来,Django已经弃用并删除了
pre_syncdb
信号。我已经更新了答案以适应更新的版本。新版本的基本机制是相同的,因为这两个方法都依赖于信号和仅在HSTORE扩展不存在时执行的SQL代码。姜戈1.8+
自从Django引入DB迁移以来,} 的新信号,可以用同样的方式使用。
pre_syncdb
信号是marked deprecated in 1.7和completely removed in 1.9。然而,他们引入了一个名为^{示例:
Django 1.6+(原始答案)
确保HSTORE扩展在
./manage.py syncdb
期间安装的一种方法是利用pre_syncdb
文件中的models.py
信号,该文件是用Django 1.6引入的。示例:
我发现如果你不想为每个新的数据库实例运行它,这是很有用的。此方法也适用于测试数据库设置期间的Django单元测试。
关于Django中信号钩子的更多信息: https://docs.djangoproject.com/en/1.6/ref/signals/#management-signals
Django现在包括a migration operation to create the ^{} extension in PostgreSQL :
我最终发现没有为我使用的特定数据库安装hstore扩展:
我以为在hstore安装之后创建的数据库会包含扩展。似乎不是这样,am I misinterpreting how extensions work ? Are they database-specific ?
相关问题 更多 >
编程相关推荐