Repository navigation
Conversation
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.
[Medium impact] The code appears safe to merge; the remaining release-note feedback is non-blocking. Findings
SummaryThe PR changes how generated migrations carry Python defaults into added columns.
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`. | |||
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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>
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
All Submissions:
Type of change
Changes To Core Features:
Description
Adding a column with an Enum default to a table that has rows, like
priority: Priority = Priority.LOW, makesreflex db makemigrationscrash withCompileError: 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 naivedatetime.now()in aUTCDateTimecolumn 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.