Opened 7 years ago
Closed 5 years ago
#28816 closed Bug (fixed)
Silent data loss when decreasing the max_length of a CharField
Reported by: | Gavin Wahl | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a CharField that I want to decrease the max_length on. I change the value and generate a migration, then run it on my database.
Desired:
I should get an error from postgres if existing data doesn't fit in the new length:
ERROR: value too long for type character varying(5)
Observed:
Any existing data longer than the new_max length is truncated and lost.
This behavior was introducted in #25002, which unconditionally added a cast to the new type. This changes the default behavior of postgres (which is to never lose data), to implicitly truncate. Django should not lose data by default.
Change History (15)
comment:1 by , 7 years ago
Component: | Database layer (models, ORM) → Migrations |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 6 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
comment:4 by , 6 years ago
Patch needs improvement: | unset |
---|
comment:5 by , 5 years ago
Status: | new → assigned |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Version: | 1.11 → master |
comment:6 by , 5 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
follow-up: 9 comment:7 by , 5 years ago
This solution doesn't prevent silencing data loss when decreasing CharField.max_length
for a base field of ArrayField
(see comment).
comment:9 by , 5 years ago
comment:10 by , 5 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
comment:12 by , 5 years ago
Triage Stage: | Ready for checkin → Accepted |
---|
comment:13 by , 5 years ago
Patch needs improvement: | set |
---|
comment:14 by , 5 years ago
Patch needs improvement: | unset |
---|
The patch was changed based on @felixxm comment on the PR.
comment:16 by , 5 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
In 1378d665: