如何修复:取消应用之前删除的迁移

2024-04-30 01:44:20 发布

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

我正在Django中更改一个应用程序名称,并遇到这个InconsistentMigrationHistory异常

我做的第一件事,如this SO answer中所述,是更改应用程序目录的名称和对应用程序的所有引用

我在django_content_type表和django_migrations表中使用了一些SQL语句将account重命名为authentication。我还重命名了此应用程序型号的表

完成这些操作后,运行python manage.py makemigrationspython manage.py migrate我会得到如下所示的InconsistentMigrationHistory异常

回溯:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "/Users/ramonaspence/.local/share/virtualenvs/chefs-notebook-2vB4UyAE/lib/python3.7/site-packages/django/db/migrations/loader.py", line 306, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration account.0001_initial is applied before its dependency authentication.0001_initial on database 'default'.

这里,account是旧的应用程序名,authentication是新的应用程序名

除了这个例外,我的第一个想法是在account中必须有一个迁移文件,它使用authentication.0001_initial迁移作为依赖项。然而,这种迁移并不存在,在我的数据库中也不存在

出于这个原因,我排除了these solutions

我已经从django_migrations表中删除了account迁移,但是运行showmigrations命令将应用app account的迁移

当我运行命令时 python manage.py migrate -fake account zeropython manage.py migrate account zero 我也犯了同样的错误

我是否需要完全删除此应用的迁移记录?或者是否有其他方法取消应用这些迁移

编辑:我的问题似乎是在取消应用迁移文件之前删除了它们


Tags: djangoinpyshareliblocallinesite