question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make enum column to work with SQLModel?

See original GitHub issue

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 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:closed
  • Created 2 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
jonra1993commented, May 23, 2022

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 not String, you modify the column type in the generated migration script.

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.

5reactions
yasamokacommented, Sep 16, 2021

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 not String, you modify the column type in the generated migration script.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found