Re: [Django] #36166: Backwards migration to replaced migration leads to InconsistentMigrationHistory on forward migrate

10 views
Skip to first unread message

Django

unread,
Feb 4, 2025, 3:57:08 AM2/4/25
to django-...@googlegroups.com
#36166: Backwards migration to replaced migration leads to
InconsistentMigrationHistory on forward migrate
------------------------------------+------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Comment (by Klaas van Schelven):

> it would be named 0001_initial_squashed_0002_foomodel_bar.py

Not in my world (Django 5.1):

{{{
(freshdjango) $ python manage.py squashmigrations squashme 0002
Will squash the following migrations:
- 0001_initial
- 0002_foomodel_bar
Do you wish to proceed? [y/N] y
Optimizing...
Optimized from 2 operations to 1 operations.
Created new squashed migration
/mnt/datacrypt/dev/squashtest/squashme/migrations/0001_squashed_0002_foomodel_bar.py
You should commit this migration but leave the old ones in place;
the new migration will be used for new installs. Once you are sure
all instances of the codebase have applied the migrations you squashed,
you can delete them.
}}}

> When it had the original name

I'm not sure that line of reasoning matters much to the present
discussion, it's more of a separate annoyance that makes it inconvenient
(impossible even) to specify a specific migration when the full name of
one is the prefix of another... :-D

> I'm not sure it makes sense to migrate backwards to replaced migrations
(and we could error instead of allowing it)

I'm not sure either... but:

> Possibly related: #24900

"Allowed migrating backward to squashed migrations" is the name of the PR,
so it seems this is indeed the ambition
--
Ticket URL: <https://code.djangoproject.com/ticket/36166#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 4, 2025, 4:08:47 AM2/4/25
to django-...@googlegroups.com
#36166: Backwards migration to replaced migration leads to
InconsistentMigrationHistory on forward migrate
------------------------------------+------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* cc: Jacob Walls (added)

Comment:

> Not in my world (Django 5.1):

Ah apologies, I had ran `python manage.py squashmigrations squashme 0001
0002` which had the different name. Replicated

> I'm not sure that line of reasoning matters much to the present
discussion

I was thinking that there were additional manual steps here (which I
believe would be relevant as it would reduce the likelihood of someone
encountering the error), but the commands being ran to create the
migration were different.

cc-ing Jacob as he was involved in that PR
--
Ticket URL: <https://code.djangoproject.com/ticket/36166#comment:3>

Django

unread,
Jan 3, 2026, 10:40:43 AMJan 3
to django-...@googlegroups.com
#36166: Backwards migration to replaced migration leads to
InconsistentMigrationHistory on forward migrate
------------------------------------+------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Comment (by VIZZARD-X):

I have submitted a PR that patches `MigrationLoader` to explicitly add
these dependency links when a replacement node is encountered in the
graph. I have also added a regression test in
`tests/migrations/test_loader.py`.
PR: https://github.com/django/django/pull/20490
--
Ticket URL: <https://code.djangoproject.com/ticket/36166#comment:4>

Django

unread,
Jan 3, 2026, 10:41:05 AMJan 3
to django-...@googlegroups.com
#36166: Backwards migration to replaced migration leads to
InconsistentMigrationHistory on forward migrate
------------------------------------+------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Changes (by VIZZARD-X):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36166#comment:5>

Django

unread,
Jan 9, 2026, 1:08:29 PMJan 9
to django-...@googlegroups.com
#36166: Backwards migration to replaced migration leads to
InconsistentMigrationHistory on forward migrate
------------------------------------+------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Comment (by HarshAlreja):

I have verified the patch provided in PR #20490. I wrote a reproduction
script that simulates the migration squash and rollback scenario.

On main: The bug was reproducible (Migration 0003 remained applied after
rollback).

With PR #20490: The bug is fixed (Migration 0003 was correctly unapplied).

Verified on Windows/SQLite. The patch works as expected.
--
Ticket URL: <https://code.djangoproject.com/ticket/36166#comment:6>
Reply all
Reply to author
Forward
0 new messages