Skip to content
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

Closed
7 of 8 tasks
wholmen opened this issue Jun 7, 2022 · 3 comments
Closed
7 of 8 tasks

Support discriminated union #356

wholmen opened this issue Jun 7, 2022 · 3 comments
Labels
answered feature New feature or request

Comments

@wholmen
Copy link

wholmen commented Jun 7, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Literal, Union, Optional
from sqlmodel import SQLModel, Field


class Bar(SQLModel):
    class_type: Literal["bar"] = "bar"
    name: str


class Foo(SQLModel):
    class_type: Literal["foo"] = "foo"
    name: str
    id: Optional[int]


class FooBar(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    data: Union[Foo, Bar] = Field(discriminator="class_type")

Description

  • No support for Pydantic 1.9's implementation of discriminated union

Wanted Solution

Would like to have support for discriminated union in SQLModel-classes

Wanted Code

from typing import Literal, Union, Optional
from sqlmodel import SQLModel, Field


class Bar(SQLModel):
    class_type: Literal["bar"] = "bar"
    name: str


class Foo(SQLModel):
    class_type: Literal["foo"] = "foo"
    name: str
    id: Optional[int]


class FooBar(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    data: Union[Foo, Bar] = Field(discriminator="class_type")

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

@ljluestc
Copy link

ljluestc commented Sep 3, 2023

from sqlmodel import SQLModel, Field, Relationship
from typing import Optional

class Base(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class_type: str

class Bar(Base):
mapper_args = {"polymorphic_identity": "bar"}
name: str

class Foo(Base):
mapper_args = {"polymorphic_identity": "foo"}
name: str

class FooBar(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
data: Relationship[Base] = Field(discriminator="class_type")

Create instances of Bar and Foo

bar_instance = Bar(name="My Bar")
foo_instance = Foo(name="My Foo")

Create instance of FooBar with either a Bar or Foo instance

foobar_instance = FooBar(data=bar_instance) # Or Foo instance

print(bar_instance)
print(foo_instance)
print(foobar_instance)

@tiangolo
Copy link
Member

Support for this was added in #440

You will be able to use it in the next version: 0.0.10 🎉

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

Copy link

github-actions bot commented Nov 6, 2023

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.

@github-actions github-actions bot closed this as completed Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants