根据http://djangonauts.github.io/django-hstore/#_limitations的说法,要在template1数据库上安装PostgreSQL的hstore扩展,必须运行
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'),
...
]当我尝试python manage.py migrate时,仍然会遇到type "hstore" does not exist错误。这个特定的堆栈跟踪来自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中创建扩展模块的迁移
发布于 2018-02-08 05:12:16
最后,我没有将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),
),
]通过这些操作,我能够成功地将其部署到可应用程序上(即,在没有预先安装hstore的情况下部署数据库)。
https://stackoverflow.com/questions/48672234
复制相似问题