FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

✨ Add `IntEnum` type that allows storing values of enum as integers in database by sunkunxi · Pull Request #1337 · fastapi/sqlmodel · GitHub

Repository navigation

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

Open
sunkunxi wants to merge 13 commits into
fastapi:mainfrom
sunkunxi:feat/IntEnum
Open

sunkunxi wants to merge 13 commits into
fastapi:mainfrom
sunkunxi:feat/IntEnum

Conversation

sunkunxi commented Apr 3, 2025 •
edited by YuriiMotov
Loading

Copy link
Copy Markdown

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 changed the title Add IntEnum for sqltypes ✨ Add IntEnum for sqltypes Apr 4, 2025
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
Copy Markdown
Author

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

Thanks to let me know

This comment was marked as outdated.

Copy link
Copy Markdown
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 Bot removed the waiting label Sep 22, 2025

YuriiMotov left a comment •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 changed the title ✨ Add IntEnum for sqltypes ✨ Add IntEnum type that allows storing values of enum as integers in database Oct 8, 2025

Copy link
Copy Markdown
Contributor

I like this

github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 28, 2025

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

I think they could create a sa_type that stores the value in the database; the SQL type will be determined by mapping it.
The name for this sa_type could be: ValueEnum

Alternatively, you could reuse sqlmodel.Enum and add a parameter to make it save the value, but that would imply that it determines the value through class introspection.

github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Jan 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has a merge conflict that needs to be resolved.

github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 21, 2026
Adds tests to ensure that integer enum fields are correctly
handled in both Postgres and SQLite DDL generation.
github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Sep 23, 2026

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

5 participants


Back | FazBrowse Home | New Git URL