Skip to content

fix(db): don't crash makemigrations on Enum or JSON column defaults - #7958

Open
ShaanGS wants to merge 2 commits into
reflex-dev:mainfrom
ShaanGS:fix/migration-unrenderable-default
Open

ShaanGS wants to merge 2 commits into
reflex-dev:mainfrom
ShaanGS:fix/migration-unrenderable-default

Conversation

@ShaanGS

@ShaanGS ShaanGS commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

Adding a column with an Enum default to a table that has rows, like priority: Priority = Priority.LOW, makes reflex db makemigrations crash with CompileError: No literal value renderer is available for literal value "<Priority.LOW: 'low'>" with datatype NULL, and no revision is written. A JSON list or dict default (tags: list = sqlmodel.Field(default=[], sa_type=sa.JSON)) fails the same way. It happens on 0.11.0 and main.

env.py builds the server default with sqlalchemy.literal(value), which has no type for these values. It now tries the column's type first, so an Enum member is written the way the column stores it (server_default='LOW'), and falls back to the untyped literal, so values that work today render the same. A naive datetime.now() in a UTCDateTime column is one of those.

A JSON value has no literal form either way. A nullable column then gets no server default, and a NOT NULL one is refused with an error that names it, the same way #7815 handles a NOT NULL column with only a default_factory. I didn't write the JSON as a string, because MySQL doesn't accept a literal default on a JSON column.

The new test fails on main and passes with the fix. The full unit suite passes and pre-commit is clean.

View guided diff

sqlalchemy.literal(value) has no type for an Enum member or a JSON list or dict, so makemigrations crashed with a CompileError. The literal now takes the column's type first, which writes an Enum member the way the column stores it, and falls back to the untyped literal where the type rejects the value. A value with neither form leaves a nullable column without a server default, and a NOT NULL column is refused, like one filled only by a default_factory.
@ShaanGS
ShaanGS requested a review from a team as a code owner November 21, 2026 21:28
@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The code appears safe to merge; the remaining release-note feedback is non-blocking.

Findings

  1. P2 Existing apps miss the fix ▶

Summary

The PR changes how generated migrations carry Python defaults into added columns.

  • Migration generation now handles defaults the column type cannot render.

Reviews (2) · Last reviewed commit: "Render a column default through the colu..." · Reviewed by Greptile

@@ -0,0 +1 @@
`reflex db makemigrations` no longer crashes with a `CompileError` when an added column defaults to an Enum member or a JSON list or dict. An Enum default becomes the server default; existing rows get NULL in a nullable JSON column, and a NOT NULL one is refused until it has a `server_default`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Existing apps miss the fix

The release note omits the adoption step for existing apps. Reflex keeps their generated alembic/env.py during upgrades, so upgrading alone leaves the Enum/JSON crash unchanged.

Tell users to back up that file, move it aside, run reflex db init, and restore any custom changes. Existing revisions are kept.

Knowledge Base Used: CLI and project workflows

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="reflex/.templates/alembic/env.py">

<violation number="1" location="reflex/.templates/alembic/env.py:162">
P2: This fix only changes the Alembic template, so existing initialized apps keep their old `env.py` and still crash on Enum or JSON defaults. Update the migration environment for existing template-based apps through a safe versioned upgrade path, or move this logic into code shared by every generated environment.</violation>

<violation number="2" location="reflex/.templates/alembic/env.py:173">
P2: When a nullable column's default has no SQL literal form, the default is dropped from the migration with no message. Existing rows will get NULL instead of the model default. Log a `logger.warning` naming the column and default so the developer can review it.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

continue
column.server_default = sqlalchemy.DefaultClause(literal)
return op
if not column.nullable:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When a nullable column's default has no SQL literal form, the default is dropped from the migration with no message. Existing rows will get NULL instead of the model default. Log a logger.warning naming the column and default so the developer can review it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/.templates/alembic/env.py, line 173:

<comment>When a nullable column's default has no SQL literal form, the default is dropped from the migration with no message. Existing rows will get NULL instead of the model default. Log a `logger.warning` naming the column and default so the developer can review it.</comment>

<file context>
@@ -112,38 +132,57 @@ def add_column_with_server_default(
+                continue
+            column.server_default = sqlalchemy.DefaultClause(literal)
+            return op
+        if not column.nullable:
+            raise missing_value_error(
+                op,
</file context>

# The column's type renders values like an Enum member as the column
# stores them; the untyped literal keeps working where the type rejects one.
for literal in (
sqlalchemy.literal(value, type_=column.type),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This fix only changes the Alembic template, so existing initialized apps keep their old env.py and still crash on Enum or JSON defaults. Update the migration environment for existing template-based apps through a safe versioned upgrade path, or move this logic into code shared by every generated environment.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/.templates/alembic/env.py, line 162:

<comment>This fix only changes the Alembic template, so existing initialized apps keep their old `env.py` and still crash on Enum or JSON defaults. Update the migration environment for existing template-based apps through a safe versioned upgrade path, or move this logic into code shared by every generated environment.</comment>

<file context>
@@ -112,38 +132,57 @@ def add_column_with_server_default(
+        # The column's type renders values like an Enum member as the column
+        # stores them; the untyped literal keeps working where the type rejects one.
+        for literal in (
+            sqlalchemy.literal(value, type_=column.type),
+            sqlalchemy.literal(value),
+        ):
</file context>

@codspeed

codspeed Bot commented Nov 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing ShaanGS:fix/migration-unrenderable-default (14850f5) with main (df7f157)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant