[英]How to install the hstore extension on the template1 database using a Django migration?
According to http://djangonauts.github.io/django-hstore/#_limitations , in order to install PostgreSQL's hstore
extension on the template1
database one must run 根据http://djangonauts.github.io/django-hstore/#_limitations ,要在
template1
数据库上安装PostgreSQL的hstore
扩展,必须运行
psql -d template1 -c 'create extension hstore;'
as an initial setup step. 作为初始设置步骤。 I'd prefer to have this automatically done by Django, however;
但是,我希望由Django自动完成此操作。 it seems I can use the CreateExtension operation for this.
看来我可以为此使用CreateExtension操作。 However, if I try to do this by modifying the
0001_initial.py
migration as follows, 但是,如果我尝试通过如下修改
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'),
...
]
I still run into a type "hstore" does not exist
error when I try to python manage.py migrate
. 当我尝试
python manage.py migrate
type "hstore" does not exist
我仍然遇到type "hstore" does not exist
错误。 This particular stack trace is from the Aptible PaaS, which is provisioned with a 'default' PostgreSQL database without hstore
installed: 此特定的堆栈跟踪来自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 -- : ^
As I understand it, template1
serves as the template for all databases when creating them, and since I am running into this error at a CREATE TABLE
command it must meant that the 0001_migration.py
did not create this extension in template1
, but in some other database. 据我了解,
template1
在创建所有数据库时用作所有数据库的模板,并且由于我在CREATE TABLE
命令中遇到此错误,因此它必须意味着0001_migration.py
并未在template1
创建此扩展名,但在其他一些目录中数据库。 How can I create a migration which does create the extension in template1
? 如何创建在
template1
中创建扩展的迁移?
I finally added the HStoreExtension()
operation not to 0001_initial.py
, but to all migrations with 'hstore' in the file (which I found by searching). 最后,我没有将
HStoreExtension()
操作添加到0001_initial.py
,而是将其添加到文件中所有带有“ hstore”的迁移中(我通过搜索找到了该迁移)。 For example, one called 0071_question_conditions.py
I modified to be as follows: 例如,我将一个名为
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),
),
]
With these operations, I'm able to deploy successfully to Aptible (ie, with a database without hstore pre-installed). 通过这些操作,我可以成功地部署到Aptible(即,在没有预安装hstore的数据库中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.