[Django] #36016: Avoid traceback when quitting makemigrations with Ctrl-C

16 views
Skip to first unread message

Django

unread,
Dec 16, 2024, 10:35:18 AM12/16/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
------------------------------+---------------------------------------
Reporter: Adam Johnson | Type: New feature
Status: new | Component: Migrations
Version: dev | 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
------------------------------+---------------------------------------
Sometimes `makemigration` asks you questions with its “questioner”. If you
realize that you missed something due to these questions, you might want
to quit with Ctrl-C and edit your models file before rerunning.

Currently, such a quit triggers Python’s default traceback:

{{{
$ ./manage.py makemigrations example
It is impossible to add a non-nullable field 'title' to book without
specifying a default. This is because the database needs something to
populate existing rows.
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a
null value for this column)
2) Quit and manually define a default value in models.py.
Select an option: ^CTraceback (most recent call last):
File "/.../manage.py", line 21, in <module>
main()
~~~~^^
File "/.../django/db/migrations/questioner.py", line 169, in
ask_not_null_addition
choice = self._choice_input(
f"It is impossible to add a non-nullable field '{field_name}' "
...<10 lines>...
],
)
File "/Users/chainz/tmp/django-makemigrations-
ctrl-c/.venv/lib/python3.13/site-
packages/django/db/migrations/questioner.py", line 114, in _choice_input
result = input()
KeyboardInterrupt
}}}

This isn’t very user-friendly: it seems like maybe you broke Django, and
it pushes the question up out of the terminal window.

Instead, we could quit with a nice message, perhaps something like:

{{{
Select an option: ^C
Cancelled, no migration file for app 'example' written.
}}}

The message needs to be clear in the case that `makemigrations` generates
some migration files but not others.

It should probably use [https://docs.djangoproject.com/en/5.1/ref/django-
admin/#syntax-coloring the notice style].
--
Ticket URL: <https://code.djangoproject.com/ticket/36016>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 16, 2024, 10:36:00 AM12/16/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
------------------------------+--------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Description changed by Adam Johnson:

Old description:
New description:
migration files for some apps but is then quit.

It should probably use [https://docs.djangoproject.com/en/5.1/ref/django-
admin/#syntax-coloring the notice style].

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

Django

unread,
Dec 16, 2024, 10:36:28 AM12/16/24
to django-...@googlegroups.com
> migration files for some apps but is then quit.
>
> It should probably use [https://docs.djangoproject.com/en/5.1/ref/django-
> admin/#syntax-coloring the notice style].

New description:

Sometimes `makemigration` asks you questions with its “questioner”. If you
realize that you missed something due to these questions, you might want
to quit with Ctrl-C and edit your models file before rerunning.

Currently, such a quit triggers Python’s default traceback:

{{{
$ ./manage.py makemigrations example
It is impossible to add a non-nullable field 'title' to book without
specifying a default. This is because the database needs something to
populate existing rows.
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a
null value for this column)
2) Quit and manually define a default value in models.py.
Select an option: ^CTraceback (most recent call last):
File "/.../manage.py", line 21, in <module>
main()
~~~~^^
...
...
...
File "/.../django/db/migrations/questioner.py", line 169, in
ask_not_null_addition
choice = self._choice_input(
f"It is impossible to add a non-nullable field '{field_name}' "
...<10 lines>...
],
)
File "/Users/chainz/tmp/django-makemigrations-
ctrl-c/.venv/lib/python3.13/site-
packages/django/db/migrations/questioner.py", line 114, in _choice_input
result = input()
KeyboardInterrupt
}}}

This isn’t very user-friendly: it seems like maybe you broke Django, and
it pushes the question up out of the terminal window.

Instead, we could quit with a nice message, perhaps something like:

{{{
Select an option: ^C
Cancelled, no migration file for app 'example' written.
}}}

The message needs to be clear in the case that `makemigrations` generates
migration files for some apps but is then quit.

It should probably use [https://docs.djangoproject.com/en/5.1/ref/django-
admin/#syntax-coloring the notice style].

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

Django

unread,
Dec 16, 2024, 10:39:11 AM12/16/24
to django-...@googlegroups.com
Instead, we could quit with minimal output, perhaps a simple message like:

{{{
Select an option: ^C
Cancelled.
}}}

If there's a message, it should probably use
[https://docs.djangoproject.com/en/5.1/ref/django-admin/#syntax-coloring
the notice style].

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

Django

unread,
Dec 16, 2024, 12:41:54 PM12/16/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
------------------------------+--------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Comment (by amansharma612):

I think one possible way to implement this would be to add an `except
KeyboardInterrupt` statement in the `_choice_input` definition in
`questioner.py` and moving the `result = input()` statement inside the try
block. I can open a PR for the same if seems reasonable.
--
Ticket URL: <https://code.djangoproject.com/ticket/36016#comment:4>

Django

unread,
Dec 16, 2024, 12:46:17 PM12/16/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: amansharma612
Type: New feature | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+-----------------------------------------
Changes (by amansharma612):

* owner: (none) => amansharma612
* status: new => assigned

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

Django

unread,
Dec 17, 2024, 3:44:42 AM12/17/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: amansharma612
Type: New feature | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+-----------------------------------------
Comment (by amansharma612):

PR - [https://github.com/django/django/pull/18947
--
Ticket URL: <https://code.djangoproject.com/ticket/36016#comment:6>

Django

unread,
Dec 17, 2024, 4:11:46 AM12/17/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | amansharma612
Cleanup/optimization | 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: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* has_patch: 0 => 1
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted
* type: New feature => Cleanup/optimization

Comment:

Thank you! Agree this makes sense, I don't think this needs documenting as
might be more of a cleanup but needs tests
--
Ticket URL: <https://code.djangoproject.com/ticket/36016#comment:7>

Django

unread,
Dec 19, 2024, 3:45:01 AM12/19/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | amansharma612
Cleanup/optimization | 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 Sarah Boyce):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin

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

Django

unread,
Dec 19, 2024, 5:33:30 AM12/19/24
to django-...@googlegroups.com
#36016: Avoid traceback when quitting makemigrations with Ctrl-C
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | amansharma612
Cleanup/optimization | 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 Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"f05edb2b43c347d4929efd52c8e2b4e08839f542" f05edb2]:
{{{#!CommitTicketReference repository=""
revision="f05edb2b43c347d4929efd52c8e2b4e08839f542"
Fixed #36016 -- Prevented traceback when quitting makemigrations with
Ctrl-C.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36016#comment:9>
Reply all
Reply to author
Forward
0 new messages