Opened 3 days ago

Last modified 42 hours ago

#37224 assigned Bug

Migration questioner complains about missing defaults for fields in unmanaged models

Reported by: Jacob Walls Owned by: Vishy
Component: Migrations Version: dev
Severity: Release blocker Keywords:
Cc: Sarah Boyce, Hanny G Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Walls)

Since the improvement to tracking unmanaged model field alterations in #35813, the migration questioner (the interactive prompt) will now complain on missing defaults for altered fields.

In a post-merge comment I suggested we could short-circuit in the questioner for unmanaged models, since if the model is unmanaged then it's not strictly true that it's "impossible" to add such fields, especially for unmanaged models that just shadow database views.

Sarah had a similar observation before merge.

With this patch, I no longer needed to enter dummy values in the prompt:

  • django/db/migrations/autodetector.py

    diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
    index 0d0010f868..b9eff8ee2b 100644
    a b class MigrationAutodetector:  
    11631163            or (field.blank and field.empty_strings_allowed)
    11641164            or (isinstance(field, time_fields) and field.auto_now)
    11651165            or (isinstance(field, auto_fields))
     1166            or not self.to_state.apps.get_model(app_label, model_name)._meta.managed
    11661167        )
    11671168        if not preserve_default:
    11681169            field = field.clone()
    class MigrationAutodetector:  
    13311332                        and not new_field.has_default()
    13321333                        and not new_field.has_db_default()
    13331334                        and not new_field.many_to_many
     1335                        and self.to_state.apps.get_model(app_label, model_name)._meta.managed
    13341336                    ):
    13351337                        field = new_field.clone()
    13361338                        new_default = self.questioner.ask_not_null_alteration(

There would be a few other places to check to see where else we can short-circuit. What do folks think?

Change History (3)

comment:1 by Jacob Walls, 3 days ago

Description: modified (diff)

comment:2 by Natalia Bidart, 3 days ago

Triage Stage: UnreviewedAccepted

Thank you!

comment:3 by Vishy, 42 hours ago

Owner: set to Vishy
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top