sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

✨ Add `IntEnum` type that allows storing values of enum as integers in database

Open KunxiSun opened this issue 8 months ago • 6 comments

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      

KunxiSun avatar Apr 03 '25 03:04 KunxiSun

Any know how to fix the lint check? It seems to be caused by code not included in my PR. image

KunxiSun avatar Apr 03 '25 09:04 KunxiSun

@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 avatar Apr 08 '25 12:04 KunxiSun

@KunxiSun: Yes - those are unrelated to your PR. We're looking into it here.

svlandeg avatar Apr 09 '25 14:04 svlandeg

@KunxiSun: Yes - those are unrelated to your PR. We're looking into it here.

Thanks to let me know

KunxiSun avatar Apr 10 '25 06:04 KunxiSun

@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.

KunxiSun avatar Sep 22 '25 08:09 KunxiSun

I like this

AlanBogarin avatar Dec 06 '25 02:12 AlanBogarin