Skip to content
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

How to deal with Postgres Enum columns? #31

Closed
8 tasks done
gregsifr opened this issue Aug 26, 2021 · 7 comments
Closed
8 tasks done

How to deal with Postgres Enum columns? #31

gregsifr opened this issue Aug 26, 2021 · 7 comments
Labels
answered question Further information is requested

Comments

@gregsifr
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import List, Optional
from enum import Enum

import sqlalchemy as sa
import sqlmodel


class Status(str, Enum):
    active = "active"
    banned = "banned"


status_enum = postgresql.ENUM("active", "banned", "c", name="status_enum")


class User(sqlmodel.SQLModel, table=True):
    __tablename__ = "auth_user"
    id: int = sqlmodel.Field(primary_key=True)
    status: Status = sqlmodel.Field(status_enum)
    password: str

Description

How to use Postgres Enums together with SQLModel?

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.3

Python Version

3.8.11

Additional Context

No response

@gregsifr gregsifr added the question Further information is requested label Aug 26, 2021
@gregsifr gregsifr changed the title How to deal with Postgres Enums? How to deal with Postgres Enum columns? Aug 26, 2021
@gregsifr
Copy link
Author

The answer:

Follow this enum template https://stackoverflow.com/a/6104825/4605629

And then use the sa_column= argument to specify the sqlalchemy column type.

@andruli
Copy link

andruli commented Aug 27, 2021

I don't think this should be closed @gregsifr .

Your solution is a good workaround but the library should be able to handle it w/o manually setting a sa_column. Closing it reduces the visibility of the issue.

@andruli
Copy link

andruli commented Aug 27, 2021

There seems to be a PR already up that tries to solve this -> #24

@Bobronium
Copy link
Contributor

@gregsifr, could you please reopen the issue for the reasons stated above?

@gregsifr
Copy link
Author

Apologies, I misunderstood and thought that since there is a PR for it there's no point to keep it open.

@gregsifr gregsifr reopened this Sep 10, 2021
synodriver pushed a commit to synodriver/sqlmodel that referenced this issue Jun 24, 2022
@tiangolo
Copy link
Member

Hey there! I think this was possibly solved in #165. That's available since SQLModel 0.0.7. Could you confirm?

@github-actions
Copy link

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants