fix(db): don't crash makemigrations on Enum or JSON column defaults
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.