Opened 9 years ago

Closed 9 years ago

Last modified 9 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 9 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by Tim Graham, 9 years ago

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

Cc: Markus Holtermann added

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

by Tim Graham, 9 years ago

Attachment: 24435-test.diff added

comment:3 by Markus Holtermann, 9 years ago

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:4 by Markus Holtermann, 9 years ago

Has patch: set

comment:5 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Markus Holtermann <info@…>, 9 years ago

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 by Markus Holtermann <info@…>, 9 years ago

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