Django InconsistentMigrationHistory:迁移 X 在其依赖于数据库“默认”之前应用 Y

Posted

技术标签:

【中文标题】Django InconsistentMigrationHistory:迁移 X 在其依赖于数据库“默认”之前应用 Y【英文标题】:Django InconsistentMigrationHistory: Migration X is applied before its dependency Y on database 'default' 【发布时间】:2018-12-07 21:46:30 【问题描述】:

这个问题类似于Django manage.py: Migration applied before its dependency 和django.db.migrations.exceptions.InconsistentMigrationHistory,只是我没有压缩迁移;我也发现这些问题的答案不是很具体。

我有一个应用程序lucy_webpython manage.py showmigrations 产生

lucy_web
 [X] 0001_initial
 [X] 0002_auto_20170614_2116
 [X] 0003_auto_20170614_2139
 [X] 0004_auto_20170614_2228
 [X] 0005_auto_20170614_2329
 [X] 0006_auto_20170615_2326
 [X] 0007_auto_20170616_1827
 [X] 0008_family_birth_parent
 [X] 0009_auto_20170621_2035
 [X] 0010_family_account_holder
 [X] 0011_auto_20170621_2218
 [X] 0012_auto_20170621_2219
 [X] 0006_auto_20170616_0008
 [X] 0007_auto_20170616_0016
 [X] 0008_auto_20170616_0441
 [X] 0009_merge_20170619_1835
 [X] 0013_merge_20170621_2225
 [X] 0010_auto_20170621_0101
 [X] 0014_merge_20170626_1858
 [X] 0010_family_user
 [X] 0014_merge_20170623_1748
 [X] 0015_merge_20170627_0605
 [X] 0016_auto_20170710_1447
 [X] 0017_auto_20170710_1448
 [X] 0018_session_expert_type
 [X] 0019_auto_20170726_1641
 [X] 0020_auto_20170727_1457
 [X] 0021_auto_20170727_1511
 [X] 0022_auto_20170727_1518
 [X] 0023_auto_20170728_1300
 [X] 0024_auto_20170730_0939
 [X] 0025_auto_20170730_1011
 [X] 0026_auto_20170731_1433
 [X] 0027_auto_20170731_2117
 [X] 0026_category
 [X] 0028_merge_20170801_0732
 [X] 0029_auto_20170801_0736
 [X] 0030_family_lucy_guide
 [X] 0031_family_baby_name
 [X] 0032_auto_20170801_2215
 [X] 0033_auto_20170801_2218
 [X] 0034_auto_20170801_2230
 [X] 0035_remove_expert_activation_date
 [X] 0035_category_inactive
 [X] 0036_merge_20170802_1543
 [X] 0037_auto_20170802_1544
 [X] 0038_auto_20170803_2127
 [X] 0039_family_profile_link
 [X] 0040_family_account_holder_string
 [X] 0041_auto_20170808_0825
 [X] 0024_expertsessiontype_packagesessiontype
 [X] 0042_merge_20170808_1302
 [X] 0043_auto_20170808_1311
 [X] 0044_auto_20170809_0750
 [X] 0045_auto_20170809_1119
 [X] 0046_auto_20170810_1159
 [X] 0047_auto_20170811_0851
 [X] 0048_auto_20170811_0851
 [X] 0049_auto_20170811_0857
 [X] 0050_auto_20170816_0821
 [X] 0051_auto_20170817_1358
 [X] 0052_auto_20170823_1317
 [X] 0053_auto_20170831_1415
 [X] 0054_session_requested_timeframe
 [X] 0055_userapn
 [X] 0055_auto_20170919_1103
 [X] 0056_merge_20170920_1206
 [X] 0057_auto_20171003_1506
 [X] 0058_auto_20171016_2025
 [X] 0059_auto_20171017_1233
 [X] 0060_auto_20171017_1448
 [X] 0061_notification
 [X] 0062_auto_20171019_1322
 [X] 0063_family_tz_string
 [X] 0064_auto_20171019_1357
 [X] 0065_auto_20171023_2251
 [X] 0066_auto_20171027_1512
 [X] 0067_auto_20171108_1719
 [X] 0067_auto_20171108_1427
 [X] 0068_merge_20171109_1418
 [X] 0069_auto_20171202_1606
 [X] 0070_auto_20171204_1217
 [X] 0071_question_conditions
 [X] 0073_auto_20171205_1112
 [X] 0067_auto_20171030_1700
 [X] 0068_merge_20171113_2335
 [X] 0069_auto_20171113_2344
 [X] 0070_auto_20171114_0113
 [X] 0071_merge_20171114_0125
 [X] 0068_merge_20171113_1117
 [X] 0069_merge_20171114_1530
 [X] 0072_merge_20171114_1731
 [X] 0074_merge_20171205_1443
 [X] 0073_auto_20171122_0830
 [X] 0075_merge_20171206_1111
 [X] 0076_auto_20171219_1036
 [X] 0077_session_proposed_time_4
 [X] 0076_auto_20171211_1625
 [X] 0078_merge_20171220_0841
 [X] 0079_session_scheuling_notes
 [X] 0080_auto_20171227_0741
 [X] 0081_auto_20171227_0754
 [X] 0082_session_recommended
 [X] 0069_auto_20171212_0637
 [X] 0078_merge_20180101_2244
 [X] 0083_merge_20180102_1840
 [X] 0084_sessiontype_session_cost
 [X] 0085_session_session_times_provided_at
 [X] 0086_auto_20180105_1501
 [X] 0087_family_recommendations_run_at
 [X] 0084_package_demo
 [X] 0088_merge_20180112_1317
 [X] 0089_session_session_times_reminded_at
 [X] 0089_family_next_outreach
 [X] 0090_merge_20180128_0227
 [X] 0091_family_adopting_or_surrogate
 [X] 0092_auto_20180206_0906
 [X] 0093_auto_20180206_1300
 [X] 0094_auto_20180206_1430
 [X] 0089_auto_20180122_1538
 [X] 0091_merge_20180201_1308
 [X] 0092_merge_20180205_1349
 [X] 0095_merge_20180206_1651
 [X] 0096_auto_20180208_1041
 [X] 0095_preactivationfamily
 [X] 0097_merge_20180208_1042
 [X] 0098_auto_20180208_1528
 [X] 0098_auto_20180208_1327
 [X] 0099_merge_20180208_1638
 [X] 0098_auto_20180208_1119
 [X] 0100_merge_20180208_1759
 [X] 0099_preactivationfamily_alternate_email
 [X] 0101_merge_20180209_1128
 [X] 0102_auto_20180213_1305
 [X] 0103_auto_20180216_1040
 [X] 0102_auto_20180215_1514
 [X] 0104_merge_20180216_1427
 [X] 0103_question_index
 [X] 0105_merge_20180216_1708
 [X] 0106_package_gift_boxes
 [X] 0107_auto_20180220_0534
 [X] 0108_remove_package_includes_boxes
 [X] 0109_family_gift_box_notification_sent_at
 [X] 0110_auto_20180221_0907
 [X] 0111_auto_20180221_0908
 [X] 0112_auto_20180221_0909
 [X] 0102_checkintype
 [X] 0103_auto_20180212_1444
 [X] 0104_checkin
 [X] 0105_auto_20180213_1433
 [X] 0106_auto_20180214_1143
 [X] 0107_auto_20180214_1601
 [X] 0108_auto_20180215_1141
 [X] 0109_merge_20180220_1039
 [X] 0110_auto_20180220_1039
 [X] 0111_merge_20180228_0759
 [X] 0113_merge_20180302_1226
 [X] 0114_family_country
 [X] 0115_auto_20180308_0737
 [X] 0116_auto_20180309_0642
 [X] 0117_auto_20180313_1110
 [X] 0118_auto_20180314_1205
 [X] 0119_auto_20180314_1206
 [X] 0120_auto_20180314_1207
 [X] 0092_auto_20180207_1125
 [X] 0110_merge_20180221_1249
 [X] 0113_merge_20180302_0919
 [X] 0114_merge_20180308_0008
 [X] 0121_merge_20180316_1850
 [X] 0122_auto_20180320_0340
 [X] 0123_auto_20180320_0341
 [X] 0124_auto_20180320_0342
 [X] 0118_company_default_package
 [X] 0122_merge_20180319_1109
 [X] 0122_auto_20180319_1739
 [X] 0123_merge_20180320_1448
 [X] 0125_merge_20180322_1639
 [X] 0126_auto_20180327_1639
 [X] 0127_auto_20180328_1621
 [X] 0125_auto_20180322_1605
 [X] 0126_merge_20180323_1632
 [X] 0128_merge_20180328_1723
 [X] 0129_auto_20180328_1731
 [X] 0126_auto_20180327_0734
 [X] 0127_merge_20180328_0155
 [X] 0130_merge_20180328_2230
 [X] 0127_auto_20180327_1638
 [X] 0128_merge_20180328_1728
 [X] 0130_merge_20180328_1845
 [X] 0131_merge_20180329_0038
 [X] 0132_auto_20180329_1045
 [X] 0129_merge_20180328_1746
 [X] 0131_merge_20180329_0938
 [X] 0133_merge_20180329_1632
 [X] 0134_auto_20180402_1026
 [X] 0135_auto_20180402_1137
 [X] 0136_sessiontype_first_trimester
 [X] 0136_auto_20180403_1321
 [X] 0137_merge_20180403_1550
 [X] 0138_auto_20180404_1316
 [X] 0136_lucyguide_guide_email
 [ ] 0137_auto_20180405_1540
 [X] 0139_merge_20180405_1700
 [X] 0140_auto_20180406_1412
 [X] 0141_auto_20180411_1757
 [X] 0142_remove_question_update_related
 [X] 0143_auto_20180411_1812
 [X] 0141_expert_phone_country
 [X] 0142_preactivationfamily_phone_country
 [X] 0144_merge_20180418_0800
 [X] 0145_sessiontype_short_description
 [X] 0146_auto_20180425_1133
 [X] 0146_auto_20180424_1119
 [X] 0147_merge_20180426_0939
 [X] 0141_auto_20180419_1028
 [X] 0146_merge_20180425_1008
 [X] 0147_merge_20180425_1522
 [X] 0148_merge_20180504_1138
 [X] 0149_auto_20180509_1345
 [X] 0150_family_adoption_date
 [X] 0151_auto_20180511_1357
 [X] 0152_auto_20180511_1403
 [X] 0153_auto_20180515_1246
 [X] 0150_auto_20180514_1410
 [X] 0151_merge_20180517_1043
 [X] 0154_merge_20180517_1753
 [X] 0155_auto_20180518_1334
 [X] 0152_package_preconception
 [X] 0156_merge_20180522_1139
 [X] 0157_auto_20180523_0610
 [X] 0150_auto_20180516_1452
 [X] 0151_merge_20180522_1134
 [X] 0158_merge_20180523_1127
 [X] 0156_lucyguide_intro
 [X] 0159_merge_20180524_1358
 [X] 0151_auto_20180518_1415
 [X] 0157_merge_20180522_1342
 [X] 0158_merge_20180522_1439
 [X] 0152_auto_20180523_1052
 [X] 0153_merge_20180523_1213
 [X] 0159_merge_20180523_1503
 [X] 0160_merge_20180524_1507
 [X] 0161_auto_20180530_2140
 [X] 0161_auto_20180529_1127
 [X] 0162_merge_20180531_0009
 [X] 0163_auto_20180627_1309
 [X] 0164_sessioncategory_name_slug
 [X] 0165_auto_20180627_populate_name_slugs

我创建了最后两个迁移(涉及name_slugs),但在我编辑它之后似乎没有运行数据迁移populate_name_slugs,所以我从django_migrations 表中删除了相应的行。

然而,现在,即使我尝试python manage.py makemigrations,我也会收到以下错误:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 28, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/kurtpeek/Documents/Dev/lucy2/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/lucy2/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/lucy2/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/lucy2/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/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 110, in handle
    loader.check_consistent_history(connection)
  File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 298, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration lucy_web.0139_merge_20180405_1700 is applied before its dependency lucy_web.0137_auto_20180405_1540 on database 'default'.

我该如何解决这个问题?我可以以某种方式简单地将父级标记为已应用吗?

更新

看起来依赖迁移甚至不在django_migrations表中:

我想把它放在这个表中可以解决这个问题。我该怎么做?

【问题讨论】:

【参考方案1】:

一般来说,可以:

注释掉 INSTALLED_APPS 中过早应用迁移的应用程序 运行迁移,允许先迁移依赖项 删除评论 再次运行迁移

【讨论】:

【参考方案2】:

我终于通过使用pg_restore 恢复数据库备份解决了这个问题。这恢复了 django_migrations 中缺失的行,并允许我迁移。

【讨论】:

【参考方案3】:

对我有用的是

注释掉有问题的迁移文件 (X.migrations) 之类的

operations = [ #... # ... ]

迁移应用拥有依赖项。

manage.py migrate &lt;Y - app_name&gt;

取消注释有问题的 X.migration

迁移有问题的应用

manage.py migrate &lt;X - app_name&gt;

这个解决方案和 Daniele Procida 的解决方案类似,但是在复杂的环境中,评论和INSTALLED_APPS 会导致很多问题。

【讨论】:

【参考方案4】:

删除相关的migrations目录并运行python3 manage.py migrate --fake。之后我做python manage.py makemigrations。最后,python3 manage.py migrate

【讨论】:

以上是关于Django InconsistentMigrationHistory:迁移 X 在其依赖于数据库“默认”之前应用 Y的主要内容,如果未能解决你的问题,请参考以下文章

Django之路

Django系列

django 错误

mac电脑安装django ,运行django报错解决

Django 大神带你飞系列~走进Django

django的文档