Migrating from TEXT to JSON in SQLite results in data loss #697
Labels
batch migrations
bug
Something isn't working
data types
great mcve
an issue with a great mcve
sqlite
Describe the bug
This is probably a SQLite bug, not really Alembic, but as there are other fixes and workarounds here to handle these cases, I thought it could be fixed here too.
When using a batch operation to migrate from a TEXT type to a JSON type Alembic copies the data using
CAST(column_name AS JSON)
to select the data to be copied, but SQLite converts anyCAST(x AS JSON)
to a literal0
.On the other side, copying the data directly, without
CAST()
works in SQLite, as it already stores the data as JSON.Note: The need to migrate while the type is not really changed underneath is to be able to also support MySQL with the same migration.
Expected behavior
I would expect/want data not to be lost. So, a TEXT field in a record containing JSON data would be preserved in a column of type JSON instead of being converted to
0
(of course, that's not Alembic's fault, but SQLite's, but it's still probably fixable here).It works "correctly" when performing the same operations without the
CAST(x AS JSON)
.To Reproduce
Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved.
See also Reporting Bugs on the website.
Log
The section that copies the data:
Printed out:
Expected (ideal):
Versions.
Additional context
Complete logs:
Note: I'm also submitting a PR addressing this.
Have a nice day!
The text was updated successfully, but these errors were encountered: