✨ Add `IntEnum` type that allows storing values of enum as integers in database
I add a IntEnum type decorator which I used in my private project.
I wish it helps!
import enum
class HeroStatus(enum.IntEnum):
ACTIVE = 1
DISABLE = 2
from sqlmodel import IntEnum
class Hero(SQLModel):
hero_status: HeroStatus = Field(sa_type=IntEnum(HeroStatus))
# Save as Integer in database, but load as IntEnum in program
user.hero_status == HeroStatus.ACTIVE
Any know how to fix the lint check? It seems to be caused by code not included in my PR.
@svlandeg Hi! Any ideas about what's going on in the Github action tests? I saw they are failed in some other new PR.
@KunxiSun: Yes - those are unrelated to your PR. We're looking into it here.
@KunxiSun, thanks for your interest and efforts!
I think this feature is quite useful. There seems to be no simple solution to configure int enums this way without creating custom
TypeDecorator. This PR will provide such solution.Please take a look at my in-code comments. Also, we need to update these tests to check statements for new field
Thanks for your code suggestion. The statement tests are added.
I also changed the type from Integer to Small Integer, cuz I think the size of Integer is too large to be needed.
I like this