-
First Check
Commit to Help
Example Codefrom typing import Union
from sqlmodel import Field, SQLModel
from typing_extensions import Literal
class Cat(SQLModel):
pet_type: Literal["cat"]
meows: int
class Dog(SQLModel):
pet_type: Literal["dog"]
barks: float
class Lizard(SQLModel):
pet_type: Literal["reptile", "lizard"]
scales: bool
class Model(SQLModel, table=True):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator="pet_type")
n: int DescriptionCreating a model containing a discriminated union, as shown in the working example in the last comment in #356, raises the exception below.
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.19 Python Version3.10.12 Additional ContextPydantic version: 2.8.2 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This question was based on a misunderstanding of how SQLModel works. There is no way for SQLModel to automatically "collapse" nested models into a top-level table model. |
Beta Was this translation helpful? Give feedback.
This question was based on a misunderstanding of how SQLModel works. There is no way for SQLModel to automatically "collapse" nested models into a top-level table model.
And, as far as I understand, discriminated unions are only available for the Pydantic-based (i.e. non-ORM) part of SQLModel.