Skip to content

✨ Allow to use Enums in custom DB schema #1146

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KrilleGH
Copy link

@KrilleGH KrilleGH commented Oct 25, 2024

I have a legacy Postgres DB which is receiving an API including new tables in a separate DB schema. The new code must not touch the existing objects in the public schema.

So all models derive from

class SchemaSQLModel(SQLModel):
    __table_args__ = {
        'schema': 'api',
    }

I have some fields which use Enums:

class HeroType(enum.StrEnum):
    normal = enum.auto()
    super = enum.auto()

class Hero(SchemaSQLModel):
    type: HeroType

The DB is PostgreSQL and I'm using the native Enum type (CREATE TYPE). Also these types reside in the custom schema.

When creating a new Hero the generated SQL has explicit casts to the types of the columns. However, for the type column only the plain name of the type is generated. Of course that doesn't work, as the Enum type isn't in the public schema.

The solution is to pass inherit_schema=True when instantiating SQLAlchemy's Enum class. I achived this by making a subclass and using that in get_sqlalchemy_type() instead of the plain one.

I'm aware this PR may not be in it's final form. Please advise what needs to be done to get it merged.

@svlandeg svlandeg added the feature New feature or request label Feb 20, 2025
@svlandeg svlandeg changed the title Allow to use Enums in custom DB schema ✨ Allow to use Enums in custom DB schema Feb 20, 2025
@svlandeg svlandeg marked this pull request as draft February 24, 2025 10:40
@KrilleGH
Copy link
Author

Hey @svlandeg, I've rebased the PR and all checks are green now. I'd be ready for review.

@svlandeg svlandeg marked this pull request as ready for review February 28, 2025 07:12
@Seluj78
Copy link

Seluj78 commented Mar 19, 2025

LGTM but what would the advantage of this be instead of using typing.Literal ?

@KrilleGH
Copy link
Author

@Seluj78, I'm not sure, where would you use typing.Literal?

My database is Postgres (I edited the description to mention this clearly) and it's possible to have different schemas there. This kind of schema is not to be confused with the structure of the database tables, also sometimes called schema. This schema is a way to group tables and other database objects. Without specification everything goes into the default public schema.

I have a lot of stuff in the public schema already which is used by a legacy application. This application is receiving a new API which is also storing some of its own data in the same database. In order to know what belongs to whom I put all the objects for the new part into a separate schema.

While SQLModel normally creates everything in the public schema, this PR allows to specify that objects should be created in a different schema.

@Seluj78
Copy link

Seluj78 commented Mar 19, 2025

Oh my, I'm really sorry I completely misread and misunderstood your PR.

After re-reading it, it totally makes sense. Enum fields should be correctly inherited into the correct schema !

Sorry again!

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