Opened 4 years ago

Closed 4 years ago

#31392 closed Cleanup/optimization (fixed)

Avoid calling SchemaEditor.effective_default() if not necessary.

Reported by: Shipeng Feng Owned by: Shipeng Feng
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Example:

def old_default_code():
    return 'old'

def new_default_code():
    # running something that is stateful, for example, increase a database counter

class Student(models.Model):
    code = models.CharField(default=new_default_code)

We change the default callable object, and run manage.py migrate, ideally new_default_code shouldn't be
called, however, we are getting the model field default value no matter whether we need it or not:

https://github.com/django/django/blob/master/django/db/backends/base/schema.py#L679

I'd love to send a pull request for this if necessary.

Change History (6)

comment:1 by Mariusz Felisiak, 4 years ago

Component: MigrationsDatabase layer (models, ORM)
Easy pickings: set
Summary: Avoid getting the model field default value during migrations if there is not a change from NULL to NOT NULLAvoid calling SchemaEditor.effective_default() if not necessary.
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Agreed, we can add this small optimization, i.e. avoid calling SchemaEditor.effective_default() if we don't change a field to a non-nullable.

comment:2 by Shipeng Feng, 4 years ago

Owner: changed from nobody to Shipeng Feng
Status: newassigned

comment:3 by Shipeng Feng, 4 years ago

Has patch: set

comment:4 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:5 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by GitHub <noreply@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 8fe2447a:

Fixed #31392 -- Avoided unnecessary SchemaEditor.effective_default() calls when altering a field.

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