Opened 5 years ago
Closed 5 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 , 5 years ago
Component: | Migrations → Database 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 NULL → Avoid calling SchemaEditor.effective_default() if not necessary. |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 5 years ago
Patch needs improvement: | set |
---|
comment:5 by , 5 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
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.