Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#22435 closed Bug (fixed)

Adding a ManyToManyField and running makemigrations prompts for a default

Reported by: andrewsg Owned by: nobody
Component: Migrations Version: dev
Severity: Release blocker Keywords:
Cc: gabrielbrasil, Sebastian Bauer Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Adding a ManyToManyField to an existing model prompts the user as so:

You are trying to add a non-nullable field 'authors' to post without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:

Since there is no way to specify a default for an M2M field in the interactive process, this makes it impossible to add M2M fields.

The workaround is to cancel and add null=True to the field definition. But null=True does not apply to ManyToManyField, so this should be unnecessary.

Change History (7)

comment:1 by Simon Charette, 11 years ago

Component: UncategorizedMigrations
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Could reproduce using master.

I'm unsure how we should solve this. It would be great if we could avoid special casing ManyToManyField here.

comment:2 by gabrielbrasil, 11 years ago

I can reproduce this too. What about forcing ManyToManyField to always be null=True?
https://github.com/django/django/pull/2563/files

That's how it behaves normally anyway, right?

comment:3 by gabrielbrasil, 11 years ago

Cc: gabrielbrasil added

comment:4 by Sebastian Bauer, 11 years ago

Cc: Sebastian Bauer added

comment:5 by Tim Graham, 11 years ago

Has patch: set
Severity: NormalRelease blocker

I didn't see a way to do this without special casing M2M: PR.

comment:6 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 3818d96426b6368e375ee4d1ccbf14d0a2c35919:

Fixed #22435 -- Prevented adding a ManyToManyField from prompting for a default.

Thanks andrewsg for the report.

comment:7 by Tim Graham <timograham@…>, 11 years ago

In f8fa735dc2a0d06e904b458633d0143820a59ac0:

[1.7.x] Fixed #22435 -- Prevented adding a ManyToManyField from prompting for a default.

Thanks andrewsg for the report.

Backport of 3818d96426 from master

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