Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KunxiSun wants to merge 17 commits into fastapi:main
base: main
Choose a base branch
Loading
from KunxiSun:feat/IntEnum

Conversation

@KunxiSun
Copy link

@KunxiSun KunxiSun commented Apr 3, 2025
edited by YuriiMotov
Loading

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 

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

This comment was marked as resolved.

This comment was marked as resolved.

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

Copy link
Member

@YuriiMotov YuriiMotov left a comment
edited
Loading

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

Reviewers

@YuriiMotov YuriiMotov YuriiMotov approved these changes

Assignees

No one assigned

Labels

feature New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /