Opened 15 months ago

Closed 14 months ago

Last modified 14 months 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, anih 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 15 months ago by charettes

  • Component changed from Uncategorized to Migrations
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

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 15 months 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 15 months ago by gabrielbrasil

  • Cc gabrielbrasil added

comment:4 Changed 15 months ago by anih

  • Cc anih added

comment:5 Changed 14 months ago by timo

  • Has patch set
  • Severity changed from Normal to Release blocker

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

comment:6 Changed 14 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 3818d96426b6368e375ee4d1ccbf14d0a2c35919:

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

Thanks andrewsg for the report.

comment:7 Changed 14 months 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