✨ Add support for Literal types by svlandeg · Pull Request #1736 · fastapi/sqlmodel
Reconstructing PR #1439, as the original author has closed this PR but @YuriiMotov and I had actually spent quite a bit of time on this already (in fact, we had completely rewritten the PR at this point 🙈).
Let me recap the history:
- Originally the PR was created to fix #57, i.e. ensure there is no crash when using a
Literaltype annotation with SQLModel. - The PR assumed that all
Literalvalues were strings (thus creating a DB column with type string), which @tiangolo pointed out wouldn't be ideal because it would be unexpected for input likeLiteral[1, 2, 3]. - Yurii then added some detection logic for all-
intand all-boolcases, to at least set the DB column type correctly. But he also raised the question of data validation. - I then pushed another update to create SQL constraints for the values in the
Literal. This way, the data can't get corrupted and the user won't receive values that don't match the expectedLiteraldefinition. - The question of having mixed types is still open-ended, we can resolve it by either (1) making all
stras "best attempt", (2) reject the input entirely or (3) some other way I'm not seeing.