Opened 5 years ago

Closed 3 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 Changed 5 years ago by Tim Graham

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

comment:3 Changed 3 years ago by Hasan Ramezani

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

comment:4 Changed 3 years ago by Hasan Ramezani

Patch needs improvement: unset

comment:5 Changed 3 years ago by Mariusz Felisiak

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

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

Resolution: fixed
Status: assignedclosed

In 1378d665:

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

comment:7 Changed 3 years ago by Mariusz Felisiak

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

comment:8 Changed 3 years ago by Simon Charette

Or decreasing the size of an ArrayField.

comment:9 in reply to:  7 Changed 3 years ago by Hasan Ramezani

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 Changed 3 years ago by Hasan Ramezani

Resolution: fixed
Status: closednew

comment:11 Changed 3 years ago by Hasan Ramezani

Reopen the ticket because of @felixxm comment.

comment:12 Changed 3 years ago by Mariusz Felisiak

Triage Stage: Ready for checkinAccepted

comment:13 Changed 3 years ago by Mariusz Felisiak

Patch needs improvement: set

comment:14 Changed 3 years ago by Hasan Ramezani

Patch needs improvement: unset

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

comment:15 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In ef4beafa:

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

comment:16 Changed 3 years ago by Mariusz Felisiak

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