Django“valueerror:找不到{migrations}的共同祖先”

2024-10-01 11:37:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图按照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时,我得到了错误:

^{pr2}$

我该怎么解决这个问题?我可以尝试将dependencies=['0001_initial']添加到Migration类中,但这似乎有点武断,因为我实际想要的是先运行这个迁移。在


Tags: djangofrompyhttpsimportdocsmigrationsdependencies
2条回答

最后,我解决了更大的问题,使用了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。在

相关问题 更多 >