#23581: Migration shouldn't generate "DROP DEFAULT" SQL if no database changes are required --------------------------------------+------------------------------------ Reporter: john_scott | Owner: nobody Type: Cleanup/optimization | Status: new Component: Migrations | Version: 1.7 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 john_scott): I would like to contribute a patch for the `DROP DEFAULT` issue if I can. My previous comments were focused just on the code added @ 2fb1939a9efae24560d41fbb7f6a280a1d2e8d06 The issue with that bit of code appears to be that _alter_field() will almost always output the `DROP DEFAULT` since: * all database backends besides MySQL will pass the `not self.skip_default(new_field)` check * most fields will also pass the `new_field.default is not None` test since if no default was actually set `new_field.default` is actually `django.db.models.fields.NOT_PROVIDED` rather than `None`. However, now that I've looked into it a bit more I'm uncertain how to proceed: * There is code which does more complex evaluation of old/new field's default situation higher up in _alter_field() https://github.com/django/django/blob/master/django/db/backends/schema.py#L545. Should the snippet of code mentioned above be folded in with this section or is there a reason it was added to near the end of the method? * The comments in #23043 suggest that no default should ever be set at the database level but the section L545 definitely does do add defaults at the db level (see output below). * Also uncertain why `add_field` would need to drop the default on a newly created field https://github.com/django/django/blob/master/django/db/backends/schema.py#L384 Here is the output of `sqlmigrate` after adding a default to a field: {{{ BEGIN; ALTER TABLE "accounts_user" ALTER COLUMN "phone" SET DEFAULT '123-123-1234'; ALTER TABLE "accounts_user" ALTER COLUMN "phone" DROP DEFAULT; COMMIT; }}} -- Ticket URL: <https://code.djangoproject.com/ticket/23581#comment:17> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.e37f359d943f3d1b7553ef10f124505d%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.