How to make enum column to work with SQLModel?
See original GitHub issueFirst 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 sqlmodel import SQLModel, Field, JSON, Enum, Column
from typing import Optional
from pydantic import BaseModel
from datetime import datetime
class TrainingStatus(str, enum.Enum):
scheduled_for_training = "scheduled_for_training"
training = "training"
trained = "trained"
class model_profile(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
training_status: Column(Enum(TrainingStatus))
model_version: str
Description
I am trying to create an enum column and use the value in the database. But I am getting this error:
RuntimeError: error checking inheritance of Column(None, Enum('scheduled_for_training', 'training', 'trained', name='trainingstatus'), table=None) (type: Column)
Does anyone knows how to help me? I tried
training_status: Column('value', Enum(TrainingStatus))
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
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
how to use Enum types with SqlModel, and alembic
I'm trying to find a way to get SqlModel and Alembic play nice ... SongType = Field( sa_column=Column( Enum(SongType), default=None, ...
Read more >Using Python enums in SQLAlchemy models
How to use enums in your models to enforce value consistency. ... In order to do so using SQLAlchemy, we have to define...
Read more >SQLModel
SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to...
Read more >MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
For example, you can create a table with an ENUM column like this: ... This means that you can use the following SELECT...
Read more >tiangolo/fastapi - Gitter
Wouldn't the safest way be to use the value from the Host header + SCRIPT_NAME ... I'm confused about whether you want to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
training_status: TrainingStatus = Field(sa_column=Column(Enum(TrainingStatus)))
Just make sure that if you’re using Alembic migration autogeneration and you require values to be stored as
Enum
in the database and notString
, you modify the column type in the generated migration script.