Repository navigation
Refuse to autogenerate a NOT NULL column filled only by a dataclass default_factory - #7815
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryRefuses to generate migrations for added NOT NULL columns whose only default is a dataclass
Reviews (7) · Last reviewed commit: "Reword the default_factory refusal messa..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 14 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
|
Generated by Claude Code |
Merging this PR will improve performance by 11.32%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing |
|
CodSpeed is red here ( Generated by Claude Code |
3faf080 to
342353a
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
fcfb9ff to
3e9f0a8
Compare
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
`mapped_column(default_factory=...)` on a MappedAsDataclass model is the dataclass's default alone: the Column has no default, so the env.py add-column hook gave the revision no server_default and adding the column to a table with rows failed with "NOT NULL constraint failed" (on sqlite batch mode leaving an `_alembic_tmp_*` table behind). The hook now looks the column up in the mapped dataclasses and, for a NOT NULL column whose only default is a default_factory, raises a CommandError naming the table and column before any revision is written (and in `alembic check`). Column defaults, literal or callable, still become the server default as before; a nullable column needs none. Fixes FINDING-012 of the 0.11.0a1 pre-release testing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
`Table.c` is keyed by `Column.key`, so a column with an explicit key other than its name was missed and its default_factory not refused. The property is now found with `mapper.get_property_by_column` and the dataclass field with `__dataclass_fields__`, instead of scanning `mapper.columns` and `dataclasses.fields`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
3e9f0a8 to
2e4a9d3
Compare
…onto main Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
|
CodSpeed on 2e4a9d3 flags Generated by Claude Code |
…tory refusal test An rx.Model table beside Base makes autogenerate compare copies of the columns, so the hook must look the column up by key. A subclass that shares its parent's table adds a column that only the subclass mapper maps. The test reached neither path before.
State the cause plainly. The old text also said the table holds rows, which is wrong for an empty table that is refused too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
…efault_factory (#7815) * Refuse a NOT NULL column filled only by a dataclass default_factory `mapped_column(default_factory=...)` on a MappedAsDataclass model is the dataclass's default alone: the Column has no default, so the env.py add-column hook gave the revision no server_default and adding the column to a table with rows failed with "NOT NULL constraint failed" (on sqlite batch mode leaving an `_alembic_tmp_*` table behind). The hook now looks the column up in the mapped dataclasses and, for a NOT NULL column whose only default is a default_factory, raises a CommandError naming the table and column before any revision is written (and in `alembic check`). Column defaults, literal or callable, still become the server default as before; a nullable column needs none. Fixes FINDING-012 of the 0.11.0a1 pre-release testing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Look up the added column by key and its property directly `Table.c` is keyed by `Column.key`, so a column with an explicit key other than its name was missed and its default_factory not refused. The property is now found with `mapper.get_property_by_column` and the dataclass field with `__dataclass_fields__`, instead of scanning `mapper.columns` and `dataclasses.fields`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Cover merged metadata and single-table inheritance in the default_factory refusal test An rx.Model table beside Base makes autogenerate compare copies of the columns, so the hook must look the column up by key. A subclass that shares its parent's table adds a column that only the subclass mapper maps. The test reached neither path before. * Reword the default_factory refusal message State the cause plainly. The old text also said the table holds rows, which is wrong for an empty table that is refused too. --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Farhan <www.mfarvirus@gmail.com> (cherry picked from commit 3184cad)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
All Submissions:
Type of change
Changes To Core Features:
Fixes FINDING-012 from the reflex 0.11.0a1 pre-release testing (artifacts on branch
claude/vibrant-pasteur-3921g3,prerelease-testing/runs/2026-11-11-v0.11.0a1/).Defect
Take a non-empty table and add
score: Mapped[int] = mapped_column(default_factory=lambda: 7)on aMappedAsDataclassmodel. Autogenerate wroteadd_column(... nullable=False)with noserver_default, soreflex db migratefailed withNOT NULL constraint failed. When the revision rebuilt the table in sqlite batch mode, alembic also left an_alembic_tmp_*table behind that broke the next migrate.The cause is the env.py template's add-column hook, which only reads
Column.default. A dataclassdefault_factoryis stored on the dataclass field only, soColumn.defaultisNone.Fix
The hook now handles a NOT NULL added column that has no
server_defaultand noColumn.defaultlike this:It looks the column up in the mapped dataclasses of the registered or detected model bases. It uses
table.c[op.column.key](keyed byColumn.key, which can differ from the name), thenmapper.get_property_by_column(column).key, then__dataclass_fields__. A mapper that doesn't map the column, as with a single-table-inheritance parent, is skipped.If the column's attribute has a
default_factory, the hook raises aCommandErrorrather than guess a value:I put the refusal at autogenerate time because it fails before a broken revision is written, so no
_alembic_tmp_*leftover can come from this path. It also fires inalembic check, so the startup schema check reports it.Docs:
docs/database/tables.mdgets a sentence aboutdefault_factoryand migrations.Known behaviour
This was confirmed by the release owner.
Column.defaultis still evaluated once, not refused. Examples are sqlmodelField(default_factory=...)andmapped_column(insert_default=callable). The value is taken when the revision is generated and becomes the server default for existing rows, as in 0.10.1.test_callable_default_is_evaluated_for_existing_rowscovers it. Only a dataclassdefault_factorythat leavesColumn.defaultempty is refused.Column.defaultstill becomes the server default, and a nullable column is never refused.Test plan
New test
test_default_factory_column_is_refusedintests/units/utils/test_migrations.py(real alembic and sqlite, MappedAsDataclass app). It failed before the fix:makemigrationsexited 0 and wrote..._factory.py. After the fix it checks:makemigrationsexits 1 with the message naming'thing.score', and no revision is written.alembic checkfails with the same message.mapped_column('points', key='points_key', default_factory=...), is refused too, naming'thing.points'. This case was added for the review thread and failed before the key fix.default=7getsserver_default=sa.text('7')and migrates the existing row.default_factorycolumn is accepted.rx.Modeltable besideBase, so autogenerate compares merged copies of the columns and the hook must find the column by key.'thing.extra'. Only the subclass mapper maps it.uv run pytest tests/units --cov(withouttests/units/reflex_bench/drivers/test_browser.py, since there is no Chromium in this sandbox): 15645 passed, 66 skipped, coverage 81.44%. After the rebase onto 93beac9, the migration and model tests pass again: 43/43 locally, and test_migrations 30/30 on SQLite 3.45.uv run ruff check .anduv run ruff format --check .are clean.uv run pyright reflex testsgives 0 errors.End to end with the verifier's
verify_dbm/v3_default_factory.sh, run on an editable install:dfmakemigrationsrc 1 with the message, no revision;migraterc 0 and its schema check logs the refusal; row untouched; no_alembic_tmp_*tabledfbatchdfdfltserver_default=sa.text('7'), rows[(1, 'a', 7)]insserver_default=sa.text('7'), rows[(1, 'a', 7)]rxdfserver_default=sa.text('7'), rows[(1, 'a', 7)]Notes
reflex db initwrote keeps the old hook until it is moved aside and re-initialized._alembic_tmp_*table left by an already-written broken revision.🤖 Generated with Claude Code
https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi