Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#24435 closed Bug (fixed)

Removing blank=True, null=True from ManyToMany field causes data deletion in migration

Reported by: Mark Tranchant Owned by: Markus Holtermann
Component: Migrations Version: 1.8beta1
Severity: Release blocker Keywords: m2m migrations deletion blank null
Cc: Markus Holtermann Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

My app has a Group model with an m2m field in it which used to look like:

things = models.ManyToManyField(Thing, blank=True, null=True)

Django 1.8a1 on my test environment warned me that the blank and null arguments were unnecessary, so I removed them:

things = models.ManyToManyField(Thing)

Then I ran manage.py makemigrations and applied the resulting migration with manage.py migrate. To my horror, the data in the relevant "linking" table was all deleted.

The section from the migration file looks like this:

    operations = [
        migrations.RemoveField(
            model_name='group',
            name='things',
        ),
        migrations.AddField(
            model_name='group',
            name='things',
            field=models.ManyToManyField(to='sa.Thing'),
        ),
    ]

This doesn't look like the right behaviour to me, and cost me a lot of time recovering data from backup. Have I done something wrong (other than trusting the migration file without inspecting it) or is this a genuine bug?

Attachments (1)

24435-test.diff (1.7 KB) - added by Tim Graham 4 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version: 1.71.8beta1

Seems to be a regression in 1.8 as 1.7 creates a single AlterField operation.

comment:2 Changed 4 years ago by Tim Graham

Cc: Markus Holtermann added

Bisected the regression to 623ccdd598625591d1a12fc1564cf3ef9a87581f. Attaching a regression test.

Changed 4 years ago by Tim Graham

Attachment: 24435-test.diff added

comment:3 Changed 4 years ago by Markus Holtermann

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:4 Changed 4 years ago by Markus Holtermann

Has patch: set

comment:5 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:6 Changed 4 years ago by Markus Holtermann <info@…>

Resolution: fixed
Status: assignedclosed

In a9e29fae105d1ddd4e0ac2059cbe62b0ee348bc8:

Fixed #24435 -- Prevented m2m field removal and addition in migrations when changing blank

Thanks Mark Tranchant for the report an Tim Graham for the test and
review.

comment:7 Changed 4 years ago by Markus Holtermann <info@…>

In bff446c205e0c4d436a5147906397ab7534343f3:

[1.8.x] Fixed #24435 -- Prevented m2m field removal and addition in migrations when changing blank

Thanks Mark Tranchant for the report and Tim Graham for the test and
review.

Backport of a9e29fae105d1ddd4e0ac2059cbe62b0ee348bc8 from master

Note: See TracTickets for help on using tickets.
Back to Top