Skip to content

Conversation

KunxiSun
Copy link

@KunxiSun KunxiSun commented Apr 3, 2025

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

This comment was marked as resolved.

@svlandeg svlandeg changed the title Add IntEnum for sqltypes ✨ Add IntEnum for sqltypes Apr 4, 2025
@svlandeg svlandeg added the feature New feature or request label Apr 4, 2025
@KunxiSun

This comment was marked as resolved.

@svlandeg

This comment was marked as resolved.

@KunxiSun
Copy link
Author

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

Thanks to let me know

YuriiMotov

This comment was marked as outdated.

KunxiSun and others added 4 commits September 22, 2025 15:11
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Updates the `IntEnum` SQLAlchemy type to `SmallInteger`.

This change reduces the storage size required for `IntEnum` columns when the full range of `Integer` is not needed.
Adds tests to ensure that integer enum fields are correctly
handled in both Postgres and SQLite DDL generation.
@KunxiSun
Copy link
Author

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

@github-actions github-actions bot removed the waiting label Sep 22, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks interesting!
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.

Implementation and tests look good to me.

We may want to describe this in docs, but maybe it's better to wait for Sebastian to validate the idea first

@YuriiMotov YuriiMotov changed the title ✨ Add IntEnum for sqltypes ✨ Add IntEnum type that allows storing values of enum as integers in database Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants