-
-
Notifications
You must be signed in to change notification settings - Fork 666
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 make enum column to work with SQLModel? #96
Comments
Just make sure that if you're using Alembic migration autogeneration and you require values to be stored as |
This worked for me. However, I think it would be a bit nicer if we could just specify
or similar as the type. |
Addresses issues fastapi#96 & fastapi#164
Thank you! |
Thanks @yasamoka This solution worked for me. Just one thing Enum on training_status: TrainingStatus = Field(sa_column=Column(Enum(TrainingStatus))) is imported from sqlmodel and Enum on TrainingStatus is imported from enum.Enum I lost some time until I realized this. |
I do not get how I should "require values to be stored as I'm getting an error which looks like sqlmodel is trying to save the enum as a string:
|
Here is a sample migration from a project of mine that shows how this is done. This is a migration script that changes a column from String to Enum.
|
do I really need a working alembic setup if I start from scratch? I can see that the table is created correctly but for some reason sqlmodel / psycopg2==2.9.3 are creating wrong queries. This is what the db looks like:
Does alembic change how sqlmodel/sqlalchemy generates statements and passes the parameters to that? |
What do you mean by starting from scratch? Do you mean that you have an existing database and you have yet to setup Alembic for migrations? If so, then no, you do not need a working alembic setup if you can manually define your column type correctly on the database schema itself directly. Alembic doesn't change anything related to how SQLModel / SQLAlchemy generates statements. Alembic compares your existing database against the schema it reads from your code and generates schema migrations based on that. Regardless of whether those migrations are applied, it is SQLAlchemy that ultimately generates statements based on your models and tables as defined in your code. If there is still a mismatch between your actual database and the models / tables you have defined, then you get an error. |
From scratch I mean that I create an empty postgresql server and use the
Maybe alembic works by correcting that enum. |
The Alembic migration I showed you was not autogenerated. It was written manually ( |
Hello, @alhoo I had the same problem and I noticed that the error happens if the table you are adding the new enum column already exists. In my case, I deleted the table and created a new migration and it worked, new types were created. I am not sure why alembic does not create a new enum type automatically on existing tables. This is not the best solution but works. |
The following solution worked fine for me:
|
Thanks for the help here everyone! 👏 🙇 If that solves the original problem, then you can close this issue @haja-k ✔️ |
Thanks for the help everyone :) |
@jonra1993 Using this approach how would you set a default value, either/both at the database level and sqlmodel level EDIT: Got it, you can just set a default inside the |
The solution provided here doesn't automatically create the enum type in case it is missing with postgresql, at least with my setting:
I am calling |
@turangojayev could you please provide more details for your proposed fix? E.g. when and where exactly do you call |
sorry for late response, well, I basically do
|
If you want enum types to be recognized by alembic, do the following.
Add import to envy.py When you update enum types, alembic will detect the changes and automatically generate migrations.
|
Can we change the scheme of where the Enum type is stored? |
I prefre to use training_status: TrainingStatus = Field(sa_type=Enum(TrainingStatus)) |
this create the following error for me:
|
@bashtian-fr I have the same issue SQLModel version 0.0.21 |
Hey there, I solved it by doing the following:
Note, that the enum class extend str AND Enum, which is from the |
First Check
Commit to Help
Example Code
Description
I am trying to create an enum column and use the value in the database. But I am getting this error:
Does anyone knows how to help me? I tried
but it doesn't seem to work as I don't understand where the 'value' should be coming from 😓 I would really appreciate any input
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.7.9
Additional Context
No response
The text was updated successfully, but these errors were encountered: