Repository navigation
fix(db): don't crash makemigrations on Enum or JSON column defaults #7958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Prompt for AI agents |
||
| 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Prompt for AI agents |
||
| 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 | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The release note omits the adoption step for existing apps. Reflex keeps their generated
alembic/env.pyduring 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!