[Django] #28816: Silent data loss when decreasing the max_length of a CharField

32 views
Skip to first unread message

Django

unread,
Nov 17, 2017, 4:53:40 PM11/17/17
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
-------------------------------------+-------------------------------------
Reporter: Gavin | Owner: nobody
Wahl |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28816>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 17, 2017, 6:02:29 PM11/17/17
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by Tim Graham):

* component: Database layer (models, ORM) => Migrations
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:1>

Django

unread,
Nov 21, 2017, 1:18:31 AM11/21/17
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+----------------------------------------
Reporter: Gavin Wahl | Owner: ChillarAnand
Type: Bug | Status: assigned

Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+----------------------------------------
Changes (by ChillarAnand):

* owner: nobody => ChillarAnand
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:2>

Django

unread,
May 11, 2019, 12:27:59 PM5/11/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani

Type: Bug | Status: assigned
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by Hasan Ramezani):

* owner: ChillarAnand => Hasan Ramezani
* needs_better_patch: 0 => 1
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:3>

Django

unread,
May 12, 2019, 9:44:03 AM5/12/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: assigned
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by Hasan Ramezani):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:4>

Django

unread,
May 21, 2019, 8:00:34 AM5/21/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------

Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by felixxm):

* status: new => assigned

* version: 1.11 => master
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:5>

Django

unread,
May 21, 2019, 8:37:29 AM5/21/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"1378d665a1c85897d951f2ca9618b848fdbba2e7" 1378d665]:
{{{
#!CommitTicketReference repository=""
revision="1378d665a1c85897d951f2ca9618b848fdbba2e7"
Fixed #28816 -- Prevented silencing data loss when decreasing
CharField.max_length on PostgreSQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:6>

Django

unread,
Sep 25, 2019, 6:37:05 AM9/25/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by felixxm):

This solution doesn't prevent silencing data loss when decreasing
`CharField.max_length` for a base field of `ArrayField` (see
[https://github.com/django/django/pull/11817#discussion_r328013173
comment]).

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:7>

Django

unread,
Sep 25, 2019, 10:04:42 AM9/25/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by Simon Charette):

Or decreasing the `size` of an `ArrayField`.

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:8>

Django

unread,
Sep 27, 2019, 9:15:18 PM9/27/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by Hasan Ramezani):

Replying to [comment:7 felixxm]:


> This solution doesn't prevent silencing data loss when decreasing
`CharField.max_length` for a base field of `ArrayField` (see
[https://github.com/django/django/pull/11817#discussion_r328013173
comment]).

[https://github.com/django/django/pull/11847 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:9>

Django

unread,
Oct 15, 2019, 3:22:36 AM10/15/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Hasan Ramezani):

* status: closed => new
* resolution: fixed =>


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:10>

Django

unread,
Oct 15, 2019, 3:24:33 AM10/15/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+---------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by Hasan Ramezani):

Reopen the ticket because of @felixxm
[https://code.djangoproject.com/ticket/28816#comment:7 comment].

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:11>

Django

unread,
Oct 15, 2019, 8:22:32 AM10/15/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------

Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by felixxm):

* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:12>

Django

unread,
Oct 15, 2019, 9:31:36 AM10/15/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:13>

Django

unread,
Oct 15, 2019, 4:15:51 PM10/15/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by Hasan Ramezani):

* needs_better_patch: 1 => 0


Comment:

The patch was changed based on @felixxm
[https://github.com/django/django/pull/11847#discussion_r334936154 comment
on the PR].

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:14>

Django

unread,
Oct 17, 2019, 7:29:51 AM10/17/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"ef4beafa2ce58a81a56ffd48a20a6153126b611a" ef4beafa]:
{{{
#!CommitTicketReference repository=""
revision="ef4beafa2ce58a81a56ffd48a20a6153126b611a"
Refs #28816 -- Prevented silencing data loss when decreasing
CharField.max_length for ArrayField.base_field on PostgreSQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:15>

Django

unread,
Oct 17, 2019, 7:30:17 AM10/17/19
to django-...@googlegroups.com
#28816: Silent data loss when decreasing the max_length of a CharField
----------------------------+------------------------------------------
Reporter: Gavin Wahl | Owner: Hasan Ramezani
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/28816#comment:16>

Reply all
Reply to author
Forward
0 new messages