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 Tim Graham, 7 years ago

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

comment:3 by Hasan Ramezani, 6 years ago

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Patch needs improvement: set

comment:4 by Hasan Ramezani, 6 years ago

Patch needs improvement: unset

comment:5 by Mariusz Felisiak, 6 years ago

Status: newassigned
Triage Stage: AcceptedReady for checkin
Version: 1.11master

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 1378d665:

Fixed #28816 -- Prevented silencing data loss when decreasing CharField.max_length on PostgreSQL.

comment:7 by Mariusz Felisiak, 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:8 by Simon Charette, 5 years ago

Or decreasing the size of an ArrayField.

in reply to:  7 comment:9 by Hasan Ramezani, 5 years ago

Replying to felixxm:

This solution doesn't prevent silencing data loss when decreasing CharField.max_length for a base field of ArrayField (see comment).

PR

comment:10 by Hasan Ramezani, 5 years ago

Resolution: fixed
Status: closednew

comment:11 by Hasan Ramezani, 5 years ago

Reopen the ticket because of @felixxm comment.

comment:12 by Mariusz Felisiak, 5 years ago

Triage Stage: Ready for checkinAccepted

comment:13 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:14 by Hasan Ramezani, 5 years ago

Patch needs improvement: unset

The patch was changed based on @felixxm comment on the PR.

comment:15 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In ef4beafa:

Refs #28816 -- Prevented silencing data loss when decreasing CharField.max_length for ArrayField.base_field on PostgreSQL.

comment:16 by Mariusz Felisiak, 5 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top