-
-
Notifications
You must be signed in to change notification settings - Fork 670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support discriminated union #356
Comments
from sqlmodel import SQLModel, Field, Relationship class Base(SQLModel, table=True): class Bar(Base): class Foo(Base): class FooBar(SQLModel, table=True): Create instances of Bar and Foobar_instance = Bar(name="My Bar") Create instance of FooBar with either a Bar or Foo instancefoobar_instance = FooBar(data=bar_instance) # Or Foo instance print(bar_instance) |
Support for this was added in #440 You will be able to use it in the next version: from 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):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator="pet_type")
n: int |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
First Check
Commit to Help
Example Code
Description
Wanted Solution
Would like to have support for discriminated union in SQLModel-classes
Wanted Code
Alternatives
No response
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10.4
Additional Context
Pydantic has added support for discrimated unions, something I use in my project with Pydantic + SQLAlchemy.
I want to switch to SQLModel, but cannot switch before discriminated unions
The text was updated successfully, but these errors were encountered: