Opened 4 weeks ago

Last modified 4 weeks 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

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

comment:1 Changed 4 weeks ago by Tim Graham

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

comment:2 Changed 4 weeks ago by ChillarAnand

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