﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37528	makemigrations writes invalid Python for a zero-valued IntFlag default	Cleisson	Cleisson	"`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`:
{{{
#!python
SECRET_KEY = ""fixture""
INSTALLED_APPS = [""sample""]
DATABASES = {""default"": {""ENGINE"": ""django.db.backends.sqlite3"", ""NAME"": "":memory:""}}
}}}

`sample/models.py`:
{{{
#!python
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 [https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing 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.
"	Bug	assigned	Migrations	dev	Low				Unreviewed	0	0	0	0	0	0
