Failure creating tables with Annotated[None,...]
in unions
#841
Unanswered
nkrishnaswami
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I am trying to serve SQLModel models via FastAPI. However, when a field has type
Optional[TheType]
, the OpenAPI schema generated uses ananyOf
TheType
ornull
(field with value either TheType or an explicit null), which isn’t what I meant and is somewhat less usefully consumed by OpenAPI tooling, e.g. no drop-down for enumerations in the docs.I tried mitigating this by switching from
Optional[TheType]
toTheType | SkipJsonSchema[None]
, ie usingSkipJsonSchema
from thepydantic.json_schema
package (importantly, equivalent totyping.Annotated[NoneType, SkipJsonSchema()]
). But after that,SQLModel.metadata.create_all
started to fail in two different ways:ValueError: Cannot have a Union of None and None as a SQLAlchemy field
in (SQLModel's)_compat.py:get_relationship_to
. (This is surprising since I’d expect both of the union members to beis not NoneType
; e.g., args of(int, typing.Annotated[NoneType, 1])
; in fact it appears to be a missingelif
in the pydantic v2get_relationship_to
union branch.).ValueError: Cannot have a (non-optional) union as a SQLlchemy field
in_compat.py:get_type_from_field
. (This is less surprising since I’d expect both union members to beis not NoneType
, so the union would fail the one-must-be-NoneType
test. This function does not check for bothNoneType
)This is perhaps a bit niche, but would there be interest in adding support for annotated
NoneType
/accepting a PR? Thanks!Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.16
Python Version
Python 3.11.7
Additional Context
The alternate (non-relationship) testcase:
Beta Was this translation helpful? Give feedback.
All reactions