我试图按照https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/中的文档创建一个迁移,该迁移实际上将对数据库执行CREATE EXTENSION IF NOT EXISTS hstore;
的SQL语句。我尝试将以下名为create_extension_hstore.py
的文件添加到migrations
目录中:
from django.db import migrations
from django.contrib.postgres.operations import CreateExtension
class Migration(migrations.Migration):
operations = [CreateExtension(name='hstore')]
我的“心理模型”是,既然Django从它们的dependencies
推断出迁移的顺序,而这个没有,它应该首先运行。但是,当我尝试运行python manage.py makemigrations --merge
时,我得到了错误:
我该怎么解决这个问题?我可以尝试将dependencies=['0001_initial']
添加到Migration
类中,但这似乎有点武断,因为我实际想要的是先运行这个迁移。在
最后,我解决了更大的问题,使用了HStoreField,而不必在数据库上手动运行
CREATE EXTENSION hstore;
,方法是将HStoreExtension()
操作添加到0001_initial.py
迁移的operations
;另请参见https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/#create-postgresql-extensions。在如果希望在第一次生成迁移之前运行它,我想您应该将此迁移添加到0001中的依赖项中。在
如果您现在只需要hstore,而不需要它先运行,那么您可以很容易地像正常一样添加一个data migration。在
相关问题 更多 >
编程相关推荐