Opened 3 years ago
Closed 3 years ago
#33471 closed Cleanup/optimization (fixed)
AlterField operation should be noop when adding/changing choices on SQLite.
Reported by: | David Szotten | Owned by: | Sarah Boyce |
---|---|---|---|
Component: | Migrations | Version: | 4.0 |
Severity: | Normal | Keywords: | |
Cc: | Adam Johnson, Sarah Boyce | 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
while writing a test case for #33470 i found that for sqlite, even a seemingly db-transparent change like adding choices
still generates sql (new table + insert + drop + rename) even though this shouldn't be needed. on e.g. postgres the same migration generates no sql
Change History (15)
comment:1 by , 3 years ago
Easy pickings: | set |
---|---|
Summary: | sqlite no-op migrations → AlterField operation should be noop when adding/changing choices. |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 3 years ago
Type: | Bug → Cleanup/optimization |
---|
follow-up: 7 comment:3 by , 3 years ago
oh i didn't realise that this was done globally by attr name. (though how come pg does the right thing?)
could marking choices
as a non_database_attrs
adversely impact third party fields that eg use a pg enum type to model choices
values?
comment:4 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Hey there, I'm new to this and trying to get into contributing to Django, but I would like to pick this ticket up and prepare a patch for it if no one is working on it right now. Please let me know if there is a problem with this, thanks!
comment:5 by , 3 years ago
Just made a pull request with the patch to fix this https://github.com/django/django/pull/15404
comment:6 by , 3 years ago
Has patch: | set |
---|
comment:7 by , 3 years ago
Cc: | added |
---|---|
Patch needs improvement: | set |
Summary: | AlterField operation should be noop when adding/changing choices. → AlterField operation should be noop when adding/changing choices on SQLite. |
Replying to David Szotten:
could marking
choices
as anon_database_attrs
adversely impact third party fields that eg use a pg enum type to modelchoices
values?
True, this would cause a regression for-party enum fields, e.g. django-mysql's EnumField a similar field may exist for PostgreSQL or Oracle. It seems we can safely overwrite _field_should_be_altered()
only on SQLite.
comment:8 by , 3 years ago
comment:9 by , 3 years ago
Has patch: | unset |
---|---|
Patch needs improvement: | unset |
follow-up: 11 comment:10 by , 3 years ago
do you know off the top of your head what mechanism makes it a no-op on pg already?
comment:11 by , 3 years ago
Replying to David Szotten:
do you know off the top of your head what mechanism makes it a no-op on pg already?
There is nothing specific to PostgreSQL here, it's also a noop on MySQL and Oracle. It's caused by remaking tables on SQLite that is necessary in most of cases (see related ticket #32502). Overwriting _field_should_be_altered()
in the SQLite backend should do the trick.
comment:12 by , 3 years ago
Cc: | added |
---|---|
Has patch: | set |
I have added a PR which hopefully addresses the above comments: https://github.com/django/django/pull/15561
comment:13 by , 3 years ago
Owner: | changed from | to
---|
comment:14 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
It was missed in #25253 (see 9159d173c3822312c653db7ff5b9a94b14af1dca). Adding
choices
to thenon_database_attrs
should fix it:django/db/backends/base/schema.py
Would you like to prepare a patch? (a regression test is required).