sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

Add option to pass kwargs to SQLAlchemy types

Open klane opened this issue 3 years ago • 0 comments

This PR adds an argument to Field to specify arguments for SQLAlchemy types. It also moves Enum above str when determining the SQLAlchemy type so that classes that extend both Enum and str are identified as Enum. A minimal example for using enums is provided below. It avoids needing to wrap the Enum in a Column and feels more unified with the other column types.

from enum import Enum

from sqlmodel import Field, SQLModel


class MyEnum(str, Enum):
    A = 'A'
    B = 'B'
    C = 'C'


class MyTable(SQLModel, table=True):
    my_enum: MyEnum = Field(sa_type_kwargs={'create_constraint': True})

klane avatar Jun 27 '22 00:06 klane