Skip to content

Conversation

@sheinbergon
Copy link
Collaborator

@sheinbergon sheinbergon commented Dec 28, 2025

Changes

Fixes #408

Adds Python enum generation for database enum types

Native database ENUMs

  • Generates Python enum classes from native ENUM types
  • Handles named/unnamed enums with automatic deduplication
  • Use --options nonativeenums to disable

Regarding Synthetic ENUMs for VARCHAR columns with CHECK Constraints

  • Retain existing capability of detecting CHECK (column IN ('val1', 'val2', ...)) patterns and converts them to Python enums.
  • Preserves original CHECK constraint for database validation
  • Use --options nosyntheticenums to disable

Generated Code

class UsersStatus(str, enum.Enum):
    ACTIVE = 'active'
    INACTIVE = 'inactive'
    PENDING = 'pending'

status: Mapped[UsersStatus] = mapped_column(Enum(UsersStatus), nullable=False)

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) which would fail without your patch
  • You've added a new changelog entry (in CHANGES.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

If there are no entries after the last release, use **UNRELEASED** as the version.
If, say, your patch fixes issue #123, the entry should look like this:

- Fix big bad boo-boo in task groups
  (`#123 <https://github.com/agronholm/sqlacodegen/issues/123>`_; PR by @yourgithubaccount)

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

@sheinbergon sheinbergon added this to the 4.0.x milestone Dec 28, 2025
@coveralls
Copy link

coveralls commented Dec 28, 2025

Coverage Status

coverage: 97.459% (+0.1%) from 97.36%
when pulling c26f283 on sheinbergon:python-native-enums
into 638861c on agronholm:master.

@sheinbergon sheinbergon changed the title support native python enum generation Support native python enum generation Dec 28, 2025
@agronholm
Copy link
Owner

Looks like there's a coverage drop. Can that be resolved?

@sheinbergon
Copy link
Collaborator Author

Looks like there's a coverage drop. Can that be resolved?

Ah, probably after my recent amendments to this PR. Sure, I'll fix them and ping you

@sheinbergon
Copy link
Collaborator Author

@agronholm can you have a look now?

@agronholm
Copy link
Owner

I'll review this tomorrow.

@sheinbergon
Copy link
Collaborator Author

@agronholm did you have a chance to review this PR?

@agronholm
Copy link
Owner

Doing that now.

Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

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

Some change requests, questions and comments.

@sheinbergon sheinbergon requested a review from agronholm January 9, 2026 11:51
@sheinbergon sheinbergon requested a review from agronholm January 9, 2026 12:01
Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

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

Hopefully this is the last round.

sheinbergon and others added 2 commits January 9, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate ENUM fields as python Enum types or string Literal types

3 participants