根据http://djangonauts.github.io/django-hstore/#_limitations,为了在hstore
数据库上安装PostgreSQL的template1
扩展名,必须运行
psql -d template1 -c 'create extension hstore;'
作为初始设置步骤。但是,我更喜欢Django自动完成这项工作;似乎我可以使用CreateExtension操作。但是,如果我尝试通过如下修改0001_initial.py
迁移来执行此操作,
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
from django.contrib.postgres.operations import CreateExtension
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
CreateExtension(name='hstore'),
...
]
当我尝试type "hstore" does not exist
时,我仍然遇到python manage.py migrate
错误。这个特定的堆栈跟踪来自Aptible PaaS,它提供了一个默认的'没有安装hstore
的PostgreSQL数据库:
remote: INFO -- : WAITING FOR: Run before_release commands from .aptible.yml: python3 manage.py migrate
remote: INFO -- : (0.001) SELECT typarray FROM pg_type WHERE typname = 'citext'; args=None
remote: INFO -- : (0.002)
remote: INFO -- : SELECT c.relname, c.relkind
remote: INFO -- : FROM pg_catalog.pg_class c
remote: INFO -- : LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
remote: INFO -- : WHERE c.relkind IN ('r', 'v')
remote: INFO -- : AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
remote: INFO -- : AND pg_catalog.pg_table_is_visible(c.oid); args=None
remote: INFO -- : (0.001) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
remote: INFO -- : (0.001)
remote: INFO -- : SELECT c.relname, c.relkind
remote: INFO -- : FROM pg_catalog.pg_class c
remote: INFO -- : LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
remote: INFO -- : WHERE c.relkind IN ('r', 'v')
remote: INFO -- : AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
remote: INFO -- : AND pg_catalog.pg_table_is_visible(c.oid); args=None
remote: INFO -- : (0.001) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
remote: INFO -- : Operations to perform:
remote: INFO -- : Apply all migrations: admin, auth, contenttypes, lucy_web, oauth2_provider, sessions
remote: INFO -- : Running migrations:
remote: INFO -- : CREATE TABLE "lucy_web_question" ("id" serial NOT NULL PRIMARY KEY, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL, "question_type" varchar(255) NOT NULL, "number_in_category" integer NOT NULL CHECK ("number_in_category" >= 0), "options" varchar(255)[] NOT NULL, "conditions" hstore NOT NULL, "info" hstore NOT NULL, "has_conditional_text_field" boolean NOT NULL); (params None)
remote: INFO -- : (0.002) CREATE TABLE "lucy_web_question" ("id" serial NOT NULL PRIMARY KEY, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL, "question_type" varchar(255) NOT NULL, "number_in_category" integer NOT NULL CHECK ("number_in_category" >= 0), "options" varchar(255)[] NOT NULL, "conditions" hstore NOT NULL, "info" hstore NOT NULL, "has_conditional_text_field" boolean NOT NULL); args=None
remote: INFO -- : Traceback (most recent call last):
remote: INFO -- : File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 62, in execute
remote: INFO -- : return self.cursor.execute(sql)
remote: INFO -- : psycopg2.ProgrammingError: type "hstore" does not exist
remote: INFO -- : LINE 1: ..., "options" varchar(255)[] NOT NULL, "conditions" hstore NOT...
remote: INFO -- : ^
据我了解,template1
在创建它们时充当所有数据库的模板,因为我在CREATE TABLE
命令遇到此错误,它必须意味着0001_migration.py
没有在template1
中创建此扩展名,而是在其他一些数据库中创建此扩展名。如何创建在template1
中创建扩展名的迁移?
答案 0 :(得分:0)
我最终将HStoreExtension()
操作添加到0001_initial.py
,但是添加到文件中的'hstore'的所有迁移(我通过搜索找到)。例如,我修改为0071_question_conditions.py
的一个如下:
from __future__ import unicode_literals
import django.contrib.postgres.fields
import django.contrib.postgres.fields.hstore
from django.db import migrations
from django.contrib.postgres.operations import HStoreExtension
class Migration(migrations.Migration):
dependencies = [
('lucy_web', '0070_auto_20171204_1217'),
]
operations = [
HStoreExtension(),
migrations.AddField(
model_name='question',
name='conditions',
field=django.contrib.postgres.fields.ArrayField(base_field=django.contrib.postgres.fields.hstore.HStoreField(blank=True), blank=True, null=True, size=None),
),
]
通过这些操作,我可以成功部署到Aptible(即,没有预先安装hstore的数据库)。