Skip to content

Commit

Permalink
Allow to use Enums in custom DB schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Hattemer committed Oct 30, 2024
1 parent 059970f commit 93d3756
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ def __init__(
ModelMetaclass.__init__(cls, classname, bases, dict_, **kw)


class SchemaEnum(sa_Enum):
def __init__(self, *args, **kwargs):
kwargs['inherit_schema'] = True
super().__init__(*args, **kwargs)


def get_sqlalchemy_type(field: Any) -> Any:
if IS_PYDANTIC_V2:
field_info = field
Expand All @@ -659,7 +665,7 @@ def get_sqlalchemy_type(field: Any) -> Any:

# Check enums first as an enum can also be a str, needed by Pydantic/FastAPI
if issubclass(type_, Enum):
return sa_Enum(type_)
return SchemaEnum(type_)
if issubclass(
type_,
(
Expand Down

0 comments on commit 93d3756

Please sign in to comment.