Opened 14 months ago

Last modified 14 months ago

#28816 assigned Bug

Silent data loss when decreasing the max_length of a CharField

Reported by: Gavin Wahl Owned by: ChillarAnand
Component: Migrations Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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.


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)


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 (2)

comment:1 Changed 14 months ago by Tim Graham

Component: Database layer (models, ORM)Migrations
Triage Stage: UnreviewedAccepted

comment:2 Changed 14 months ago by ChillarAnand

Owner: changed from nobody to ChillarAnand
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top