postgresql Django“ValueError:找不到{migrations}的公共祖先”

k2fxgqgv  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(3)|浏览(101)

我试图按照https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/中的文档创建一个迁移,它本质上将在数据库上执行SQL语句CREATE EXTENSION IF NOT EXISTS hstore;。我尝试将以下文件(名为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时,我得到了错误:

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py makemigrations --merge
(0.000) SELECT typarray FROM pg_type WHERE typname = 'citext'; args=None
(0.003) 
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
(0.001) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
Traceback (most recent call last):
  File "manage.py", line 29, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 142, in handle
    return self.handle_merge(loader, conflicts)
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 272, in handle_merge
    raise ValueError("Could not find common ancestor of %s" % migration_names)
ValueError: Could not find common ancestor of {'0091_family_adopting_or_surrogate', 'create_extension_hstore'}


我该如何解决此问题?我可以尝试将dependencies=['0001_initial']添加到Migration类中,但这似乎有点武断,因为我实际上想要的是在其他任何事情之前运行这个迁移。

mutmk8jj

mutmk8jj1#

我认为,如果您希望在第一次生成迁移之前运行该迁移,您可能希望将该迁移添加到0001中的依赖项中。
如果你现在需要hstore,而不需要它首先运行,你可以很容易地像平常一样在适当的地方添加数据迁移。

vkc1a9a2

vkc1a9a22#

我最终解决了更大的问题,使用HStoreField而不必在数据库上手动运行CREATE EXTENSION hstore;,方法是将HStoreExtension()操作添加到0001_initial.py迁移的operations中;也参见https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/#create-postgresql-extensions。

b1payxdu

b1payxdu3#

我首先删除所有迁移文件夹,不工作,然后我删除虚拟环境,创建一个新的venv,再次安装所有软件包-这解决了问题(我可以成功运行迁移和运行迁移,没有错误再次显示)。就像这样:
source env/bin/activate
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y\n
使无效
rm -r env/
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt

相关问题