Opened 39 minutes ago
Last modified 29 minutes ago
#37528 assigned Bug
makemigrations writes invalid Python for a zero-valued IntFlag default
| Reported by: | Cleisson | Owned by: | Cleisson |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Low | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
makemigrations succeeds when an IntegerField default is a zero-valued enum.IntFlag member, but writes models.IntegerField(default=). Django then raises a SyntaxError when it imports the migration, before it can show the migration plan.
Reproduced on Django 6.2.dev20261119224235 (main, c67d872765e05ebd009fadf7de8d834b01bae9e4), CPython 3.12.15, with in-memory SQLite.
In an empty directory, create sample/__init__.py (empty) and the following files.
settings.py:
SECRET_KEY = "fixture" INSTALLED_APPS = ["sample"] DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}
sample/models.py:
import enum from django.db import models class Options(enum.IntFlag): NONE = 0 FIRST = 1 SECOND = 2 class Example(models.Model): options = models.IntegerField(default=Options.NONE)
From that directory, with the indicated Django checkout on the Python import path, run:
export DJANGO_SETTINGS_MODULE=settings python -m django makemigrations sample --no-header python -m django migrate --plan
The first command creates sample/migrations/0001_initial.py. The second fails while importing it:
File ".../sample/migrations/0001_initial.py", line 17
('options', models.IntegerField(default=)),
^^^^^^^^
SyntaxError: expected argument value expression
The migration docs list enum.Flag instances as serializable. The generated migration should import and preserve the zero-valued default. EnumSerializer joins member lookups with | , but list(Options.NONE) is empty, so there is no expression after default=.
In fresh copies of the app, Options.FIRST loads as 1 as expected, but Options(5) also loads as 1 in migration state: both defaults serialize to Options['FIRST'], dropping the unnamed bit 4 from Options(5). Related: #33916 added support for serializing flag combinations.
I used Claude Code (Opus 5.5) and OpenCode (Sol 6.2 and Astra 6.2) for debugging, regression tests, and writing.