Opened 2 years ago

Closed 2 years ago

Last modified 2 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: master
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 Changed 2 years ago by Simon Charette

Component: UncategorizedMigrations
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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 Changed 2 years ago by gabrielbrasil

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 Changed 2 years ago by gabrielbrasil

Cc: gabrielbrasil added

comment:4 Changed 2 years ago by Sebastian Bauer

Cc: Sebastian Bauer added

comment:5 Changed 2 years ago by Tim Graham

Has patch: set
Severity: NormalRelease blocker

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

comment:6 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 3818d96426b6368e375ee4d1ccbf14d0a2c35919:

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

Thanks andrewsg for the report.

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

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