[Django] #24900: KeyError when trying to migrate to a replaced migration

27 views
Skip to first unread message

Django

unread,
Jun 2, 2015, 3:15:07 PM6/2/15
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
--------------------------------------+------------------------
Reporter: carljm | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
Django exhibits some internal confusion regarding whether replaced
migrations exist or not. Consider this simple app with two migrations and
squashed migration replacing both:

{{{
$ ls testproj/migrations/
0001_initial.py 0001_squashed_0002_thing_age.py 0002_thing_age.py
__init__.py
}}}

When it comes to disambiguating input, Django seems to believe that the
replaced migrations still need to be considered:

{{{
$ ./manage.py migrate testproj 0001
CommandError: More than one migration matches '0001' in app 'testproj'.
Please be more specific.
}}}

But if you actually try to disambiguate and specify one of the replaced
migrations, Django no longer thinks it exists (and isn't very graceful
about telling you so):

{{{
$ ./manage.py migrate testproj 0001_initial
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File
"/home/carljm/projects/django/django/django/django/core/management/__init__.py",
line 330, in execute_from_command_line
utility.execute()
File
"/home/carljm/projects/django/django/django/django/core/management/__init__.py",
line 322, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File
"/home/carljm/projects/django/django/django/django/core/management/base.py",
line 347, in run_from_argv
self.execute(*args, **cmd_options)
File
"/home/carljm/projects/django/django/django/django/core/management/base.py",
line 398, in execute
output = self.handle(*args, **options)
File
"/home/carljm/projects/django/django/django/django/core/management/commands/migrate.py",
line 135, in handle
plan = executor.migration_plan(targets)
File
"/home/carljm/projects/django/django/django/django/db/migrations/executor.py",
line 50, in migration_plan
self.loader.graph.node_map[target].children
KeyError: ('testproj', '0001_initial')
}}}

There could be several different approaches to fixing this, but my feeling
is that Django shouldn't prevent you from migrating to a replaced
migration. If a migration still exists on disk, even if it's been squashed
and you've fully migrated the squashed set, you should be able to migrate
back to a state within the squashed set. It seems like there might be
production rollback cases where that could be important, and I don't see
in principle why it shouldn't be possible.

If that turns out to be impractical, then I think Django oughtn't bother
you about resolving ambiguities with migration names it won't let you
migrate to anyway. And the "nonexistent" error for this case should be
nicer than a raw `KeyError`. (In Django 1.7 the error was "ValueError:
Node ('testproj17', '0001_initial') not a valid node", which is perhaps a
bit better, but not much.)

--
Ticket URL: <https://code.djangoproject.com/ticket/24900>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 2, 2015, 8:15:23 PM6/2/15
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+------------------------------------

Reporter: carljm | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
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 MarkusH):

* stage: Unreviewed => Accepted


Comment:

I agree, that should be possible and I think it is possible.

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:1>

Django

unread,
Feb 8, 2016, 4:47:25 AM2/8/16
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+------------------------------------

Reporter: carljm | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
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 shaib):

* cc: shaib (added)


Comment:

Just ran into this. FWIW, workaround, as long as we do '''not''' do what
comment:5:ticket:24902 recommends (which is to keep the state of squashed
migrations in the db), is to simply move the squashed migration file away,
migrate backwards to your heart's content, then bring it back.

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:2>

Django

unread,
Jun 5, 2021, 9:26:40 AM6/5/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

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 Jacob Walls):

* owner: nobody => Jacob Walls
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:3>

Django

unread,
Jun 5, 2021, 11:24:54 AM6/5/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

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 Jacob Walls):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/14495 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:4>

Django

unread,
Jun 5, 2021, 4:03:58 PM6/5/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------------

Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Shai Berger):

* stage: Accepted => Ready for checkin


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

Django

unread,
Jun 6, 2021, 2:48:59 PM6/6/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------

Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

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 Jacob Walls):

* stage: Ready for checkin => Accepted


Comment:

I added two test methods, so bumping this back from RFC. Thanks for the
review, Shai!

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:6>

Django

unread,
Jul 13, 2021, 7:18:09 AM7/13/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev

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 Asif Saifuddin Auvi):

* version: 1.8 => dev


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:7>

Django

unread,
Aug 13, 2021, 12:18:14 PM8/13/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------------

Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Shai Berger):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:8>

Django

unread,
Aug 24, 2021, 6:40:21 AM8/24/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------

Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


* stage: Ready for checkin => Accepted


Comment:

I'd like to discuss alternatives before moving forward, see
[https://github.com/django/django/pull/14495#pullrequestreview-737044138
comment].

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:9>

Django

unread,
Aug 24, 2021, 11:05:30 AM8/24/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
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 Jacob Walls):

* needs_better_patch: 1 => 0


Comment:

Updated implementation to reduce complexity.

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:10>

Django

unread,
Aug 24, 2021, 11:39:03 AM8/24/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration

----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
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
----------------------------+---------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:11>

Django

unread,
Aug 25, 2021, 4:25:34 AM8/25/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:12>

Django

unread,
Aug 25, 2021, 9:48:28 AM8/25/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration
----------------------------+---------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
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 Jacob Walls):

* needs_better_patch: 1 => 0

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:13>

Django

unread,
Aug 30, 2021, 6:10:09 AM8/30/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration
----------------------------+---------------------------------------------

Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:14>

Django

unread,
Aug 30, 2021, 7:48:16 AM8/30/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration
----------------------------+---------------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3219dd3388c437b4bd869b76ddd43c9cdad05090" 3219dd33]:
{{{
#!CommitTicketReference repository=""
revision="3219dd3388c437b4bd869b76ddd43c9cdad05090"
Fixed #24900 -- Allowed migrating backward to squashed migrations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:16>

Django

unread,
Aug 30, 2021, 7:48:17 AM8/30/21
to django-...@googlegroups.com
#24900: KeyError when trying to migrate backward to a replaced migration
----------------------------+---------------------------------------------
Reporter: Carl Meyer | Owner: Jacob Walls
Type: Bug | Status: assigned

Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9e17cc062c62eec1177aaf65ebebba43e074cde2" 9e17cc0]:
{{{
#!CommitTicketReference repository=""
revision="9e17cc062c62eec1177aaf65ebebba43e074cde2"
Refs #24900 -- Added MigrationLoader test for applying squashed
migrations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24900#comment:15>

Reply all
Reply to author
Forward
0 new messages