Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33101 closed Cleanup/optimization (duplicate)

Improve output of non-interactive migration questioner

Reported by: Hiroki Sawano Owned by: Hiroki Sawano
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The non-interactive questioner doesn't tell anything about a suppressed prompt, so it is difficult to see what happened when, for example, adding a non-nullable field to a model without specifying a default.
I'd like it to print messages as shown below, like the interactive questioner does.

$ python manage.py makemigrations --noinput
It is impossible to add a non-nullable field '...' to ... without specifying a default. This is because the database needs something to populate existing rows.

If that makes sense, I'll change ask_not_null_addition, ask_not_null_alteration, and ask_auto_now_add_addition in django.db.migrations.questioner.NonInteractiveMigrationQuestioner like this:

class NonInteractiveMigrationQuestioner(MigrationQuestioner):

    def ask_not_null_addition(self, field_name, model_name):
        # We can't ask the user, so act like the user aborted.
        print(
            f"It is impossible to add a non-nullable field '{field_name}' "
            f"to {model_name} without specifying a default. This is "
            f"because the database needs something to populate existing "
            f"rows."
        )
        sys.exit(3)

    def ask_not_null_alteration(self, field_name, model_name):
        # We can't ask the user, so set as not provided.
        print(
            f"It is impossible to change a nullable field '{field_name}' "
            f"on {model_name} to non-nullable without providing a "
            f"default. This is because the database needs something to "
            f"populate existing rows.\n"
            f"Existing rows that contain NULL values "
            f"will have to be handled manually, for example with a "
            f"RunPython or RunSQL operation."
        )
        return NOT_PROVIDED

    def ask_auto_now_add_addition(self, field_name, model_name):
        # We can't ask the user, so act like the user aborted.
        print(
            f"It is impossible to add the field '{field_name}' with "
            f"'auto_now_add=True' to {model_name} without providing a "
            f"default. This is because the database needs something to "
            f"populate existing rows."
        )
        sys.exit(3)

Change History (5)

comment:1 by Hiroki Sawano, 3 years ago

Owner: changed from nobody to Hiroki Sawano
Status: newassigned

comment:2 by Hiroki Sawano, 3 years ago

Needs tests: set

comment:3 by Mariusz Felisiak, 3 years ago

Easy pickings: unset
Needs tests: unset

Duplicate of #29470.

comment:4 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: assignedclosed

in reply to:  4 comment:5 by Hiroki Sawano, 3 years ago

Replying to Mariusz Felisiak:
Oh sorry. I couldn't find the duplicated issue. Thank for checking.

Note: See TracTickets for help on using tickets.
Back to Top