简体   繁体   English

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

[英]Django “ValueErrror: could not find common ancestor of {migrations}”

I'm trying to follow the documentation in https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/ to create a migration which will essentially perform the SQL statement CREATE EXTENSION IF NOT EXISTS hstore; 我正在尝试按照https://docs.djangoproject.com/zh-CN/2.0/ref/contrib/postgres/operations/中的文档进行操作,以创建实质上执行SQL语句的迁移( CREATE EXTENSION IF NOT EXISTS hstore; on the database. 在数据库上。 I've tried adding the following file, called create_extension_hstore.py , to the migrations directory: 我尝试将以下文件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')]

My 'mental model' of this is that since Django infer the order of the migrations from their dependencies and this one has none, it should be run first. 我的“心理模型”是,由于Django从其dependencies推断迁移顺序,而该dependencies没有dependencies ,因此应首先运行它。 However, I'm getting the error when I try to run python manage.py makemigrations --merge : 但是,当我尝试运行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'}

How can I fix this? 我怎样才能解决这个问题? I could try adding dependencies=['0001_initial'] to the Migration class but this seems a bit arbitrary as what I actually want is to run this migration before anything else. 我可以尝试在Migration类中添加dependencies=['0001_initial'] ,但这似乎有些武断,因为我真正想要的是先执行此迁移。

I think you would want to add this migration to the dependencies in 0001, if you want it run before the first generated migrations. 我想您希望将此迁移添加到0001中的依赖项,如果您希望它在首次生成的迁移之前运行。

If you are just getting to needing hstore now and don't need it to run first you can pretty easily just add as a data migration in place like normal. 如果您现在只是需要hstore而又不需要先运行它,则可以像平常一样轻松地将其添加为数据迁移

I ended up solving the larger problem, using the HStoreField without having to manually run CREATE EXTENSION hstore; 我最终使用HStoreField解决了更大的问题,而不必手动运行CREATE EXTENSION hstore; on the database, by adding the HStoreExtension() operation to the operations of the 0001_initial.py migration; 在数据库上,通过将HStoreExtension()操作添加到0001_initial.py迁移的operations中; see also https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/operations/#create-postgresql-extensions . 另请参阅https://docs.djangoproject.com/zh-CN/2.0/ref/contrib/postgres/operations/#create-postgresql-extensions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM