Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+migration-unrenderable-default.bugfix.md
Original file line number Diff line number Diff line change
@@ -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!

65 changes: 52 additions & 13 deletions reflex/.templates/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ def has_default_factory(op: ops.AddColumnOp) -> bool:
return False


def missing_value_error(
op: ops.AddColumnOp, cause: str, remedy: str
) -> util.CommandError:
"""The error for a NOT NULL column that the migration cannot fill in existing rows.
Args:
op: The add-column operation.
cause: Why the column has no value, after the column's name.
remedy: The sentence that says how to give it one.
Returns:
The error to raise.
"""
name = ".".join(filter(None, (op.schema, op.table_name, op.column.name)))
return util.CommandError(
f"Cannot add the NOT NULL column {name!r} {cause}: the migration has no "
f"value for rows that a database already holds. {remedy}"
)


@writer.rewrites(ops.AddColumnOp)
def add_column_with_server_default(
context: MigrationContext, revision: str | Iterable[str | None], op: ops.AddColumnOp
Expand All @@ -112,38 +132,57 @@ def add_column_with_server_default(
Existing rows then get that value when the column is added, so a NOT NULL
column can be added to a table that already holds data. A callable default
is evaluated once, when the migration script is generated. A dataclass
``default_factory`` is not on the column, so a NOT NULL column with one and
no other default is refused rather than added without a value.
is evaluated once, when the migration script is generated. A NOT NULL
column is refused rather than added without a value when its default has no
SQL literal form, like a JSON list, or when its only default is a dataclass
``default_factory``, which is not on the column.
Args:
context: The migration context.
revision: The revision being generated.
op: The add-column operation to rewrite.
Returns:
The same operation, with ``server_default`` filled in.
The same operation, with ``server_default`` filled in when the default
has a SQL literal form.
Raises:
CommandError: If the column is NOT NULL and its only default is a
dataclass ``default_factory``.
CommandError: If the column is NOT NULL and its default has no SQL
literal form, or its only default is a dataclass ``default_factory``.
"""
column = op.column
if column.server_default is not None:
return op
default = column.default
if default is not None:
value = default.arg(None) if default.is_callable else default.arg
column.server_default = sqlalchemy.DefaultClause(sqlalchemy.literal(value))
# 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>

sqlalchemy.literal(value),
):
try:
literal.compile(
dialect=context.dialect, compile_kwargs={"literal_binds": True}
)
except sqlalchemy.exc.CompileError:
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>

raise missing_value_error(
op,
f"with the default {value!r}, which has no SQL literal form",
"Set server_default=... or make the column nullable.",
)
elif not column.nullable and has_default_factory(op):
name = ".".join(filter(None, (op.schema, op.table_name, column.name)))
msg = (
f"Cannot add the NOT NULL column {name!r} with only a default_factory: "
"the migration has no value for rows that a database already holds. "
raise missing_value_error(
op,
"with only a default_factory",
"Set server_default=..., a literal default= or insert_default=, or "
"make the column nullable."
"make the column nullable.",
)
raise util.CommandError(msg)
return op


Expand Down
55 changes: 55 additions & 0 deletions tests/units/utils/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,61 @@ def test_callable_default_is_evaluated_for_existing_rows(project: Path):
assert created


ENUM_APP = SQLMODEL_APP.replace(
"import datetime\n", "import datetime\nimport enum\n"
).replace(
"class Thing(",
'class Level(str, enum.Enum):\n LOW = "low"\n HIGH = "high"\n\n\nclass Thing(',
)


def test_defaults_without_an_untyped_sql_literal(
project: Path, caplog: pytest.LogCaptureFixture
):
"""An Enum default is written through its column type; a JSON default has no SQL form.

A nullable JSON column is added without a server default and a NOT NULL one
is refused, since the existing rows would get no value. A value the column
type rejects, like a naive datetime in a UTC column, keeps its untyped literal.
"""
_db("init")
with get_engine().begin() as connection:
connection.execute(sa.text("INSERT INTO thing (t1) VALUES ('existing')"))
columns = (
" t1: str\n"
" level: Level = Level.HIGH\n"
" tags: list | None = sqlmodel.Field(default=[], sa_type=sa.JSON)\n"
" created: datetime.datetime = sqlmodel.Field(\n"
" default_factory=datetime.datetime.now\n"
" )"
)
_write_source(project, ENUM_APP.format(columns=columns))
_db("makemigrations", "--message", "defaults")
_db("migrate")
((_, t1, level, tags, created),) = _rows()
assert (t1, level, tags) == ("existing", "HIGH", None)
assert created
caplog.clear()

_write_source(
project,
ENUM_APP.format(
columns=f"{columns}\n"
" labels: list = sqlmodel.Field(default=[], sa_type=sa.JSON)"
),
)
_db("makemigrations", "--message", "json", exit_code=1)
assert _errors(caplog) == [
(
"Cannot add the NOT NULL column 'thing.labels' with the default [], which "
"has no SQL literal form: the migration has no value for rows that a "
"database already holds. Set server_default=... or make the column "
"nullable."
)
]
assert len(_versions(project)) == 2


DATACLASS_APP = SQLALCHEMY_APP.replace(
"from sqlalchemy.orm import DeclarativeBase,",
"from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass,",
Expand Down
Loading