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 make enum column to work with SQLModel? #96

Closed
8 tasks done
haja-k opened this issue Sep 14, 2021 · 24 comments
Closed
8 tasks done

How to make enum column to work with SQLModel? #96

haja-k opened this issue Sep 14, 2021 · 24 comments
Labels
answered question Further information is requested

Comments

@haja-k
Copy link

haja-k commented Sep 14, 2021

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

@haja-k haja-k added the question Further information is requested label Sep 14, 2021
@haja-k haja-k changed the title How to make enum column to work in with SQLModel? How to make enum column to work with SQLModel? Sep 14, 2021
@yasamoka
Copy link

yasamoka commented 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.

@JLHasson
Copy link

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.

This worked for me. However, I think it would be a bit nicer if we could just specify

training_status: Enum[TrainingStatus]

or similar as the type.

chriswhite199 added a commit to chriswhite199/sqlmodel that referenced this issue Nov 24, 2021
@haja-k
Copy link
Author

haja-k commented Dec 8, 2021

Thank you!

@jonra1993
Copy link

jonra1993 commented May 17, 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.

@alhoo
Copy link

alhoo commented May 31, 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.

I do not get how I should "require values to be stored as Enum" when using postgresql engine for example. Is there a flag somewhere or how does that work?

I'm getting an error which looks like sqlmodel is trying to save the enum as a string:

sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input value for enum objecttypes: "type1"

@yasamoka
Copy link

yasamoka commented May 31, 2022

I do not get how I should "require values to be stored as Enum" when using postgresql engine for example. Is there a flag somewhere or how does that work?

I'm getting an error which looks like sqlmodel is trying to save the enum as a string:

sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input value for enum objecttypes: "type1"

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.

"""language_fluency

Revision ID: 5342a2459462
Revises: 919490ea9497
Create Date: 2021-09-17 03:49:14.251234

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '5342a2459462'
down_revision = '919490ea9497'
branch_labels = None
depends_on = None

fluency = sa.Enum('WEAK', 'MODERATE', 'GOOD', name='fluency')


def upgrade():
    fluency.create(op.get_bind())
    op.alter_column('language', 'fluency', type_=fluency, postgresql_using='fluency::text::fluency')


def downgrade():
    op.alter_column('language', 'fluency', type_=sqlmodel.sql.sqltypes.AutoString())
    fluency.drop(op.get_bind())

@alhoo
Copy link

alhoo commented May 31, 2022

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:

                    Table "public.items"
 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 type   | itemtypes         |           |          | 
 id     | uuid              |           | not null | 
 name   | character varying |           | not null | 

Does alembic change how sqlmodel/sqlalchemy generates statements and passes the parameters to that?

@yasamoka
Copy link

yasamoka commented May 31, 2022

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.

@alhoo
Copy link

alhoo commented May 31, 2022

From scratch I mean that I create an empty postgresql server and use the SQLModel.metadata.create_all(engine) to create tables. I got this working with mysql so I guess the main issue I'm having is that sqlmodel does not create the postgresql enum correctly. Postgres shows

docker compose exec db psql -U postgres -c "select enum_range(null::itemtypes);"
 enum_range 
------------
 {}
(1 row)

Maybe alembic works by correcting that enum.

@yasamoka
Copy link

yasamoka commented May 31, 2022

From scratch I mean that I create an empty postgresql server and use the SQLModel.metadata.create_all(engine) to create tables. I got this working with mysql so I guess the main issue I'm having is that sqlmodel does not create the postgresql enum correctly. Postgres shows

docker compose exec db psql -U postgres -c "select enum_range(null::itemtypes);"
 enum_range 
------------
 {}
(1 row)

Maybe alembic works by correcting that enum.

The Alembic migration I showed you was not autogenerated. It was written manually (alembic revision -m "message").

@jonra1993
Copy link

jonra1993 commented Jun 2, 2022

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.
image

@malf2
Copy link

malf2 commented Sep 22, 2022

The following solution worked fine for me:

import enum

from sqlalchemy import (Column, Integer)
from sqlalchemy_utils import ChoiceType
from sqlmodel import SQLModel, Field

class LightType(enum.IntEnum):
    off = 0
    on = 1
    flashing = 2

class Events(SQLModel, table=True):
    green: LightType = Field(sa_column=Column(ChoiceType(LightType, impl=Integer()), nullable=False))

@tiangolo
Copy link
Member

Thanks for the help here everyone! 👏 🙇

If that solves the original problem, then you can close this issue @haja-k ✔️

@haja-k
Copy link
Author

haja-k commented Nov 15, 2022

Thanks for the help everyone :)

@haja-k haja-k closed this as completed Nov 15, 2022
@Pk13055
Copy link

Pk13055 commented Feb 23, 2023

@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 Field(), ie, Field(default=SomeEnum.foo)

@turangojayev
Copy link

turangojayev commented Jul 26, 2023

The solution provided here doesn't automatically create the enum type in case it is missing with postgresql, at least with my setting:

sqlmodel: 0.0.8
python: 3.10.11
macOS: Ventura 13.3.1 (22E261)

I am calling create manually (Enum(TrainingStatus).create(engine)) to fix this

@christianholland
Copy link

christianholland commented Jul 27, 2023

@turangojayev could you please provide more details for your proposed fix? E.g. when and where exactly do you call create?

@turangojayev
Copy link

turangojayev commented Aug 17, 2023

sorry for late response, well, I basically do


sa_TrainingStatus = Enum(TrainingStatus)

def init_db():
    sa_TrainingStatus.create(engine, checkfirst=True)
    SQLModel.metadata.create_all(engine)


@asynccontextmanager
async def lifespan(app: FastAPI):
    create_db_and_tables()
    yield


app = FastAPI(lifespan=lifespan)

@tjeaneric
Copy link

tjeaneric commented Nov 14, 2023

If you want enum types to be recognized by alembic, do the following.
Installation

  SQLModel: 0.0.18 
  Python: 3.11

pip install alembic-postgresql-enum or poetry add alembic-postgresql-enum

Add import to envy.py
import alembic_postgresql_enum

When you update enum types, alembic will detect the changes and automatically generate migrations.

def upgrade() -> None:
    # ### commands auto-generated by Alembic - please adjust! ###
    op.sync_enum_values('public', 'channel', ['USSD', 'WHATSAPP'],
                        [('subscription', 'channel')],
                        enum_values_to_rename=[])
    # ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.sync_enum_values('public', 'channel', ['WHATSAPP'],
                    [('subscription', 'channel')],
                    enum_values_to_rename=[])
# ### end Alembic commands ###

@dipsx
Copy link

dipsx commented Apr 4, 2024

Can we change the scheme of where the Enum type is stored?

@KunxiSun
Copy link

KunxiSun commented Jul 3, 2024

I prefre to use sa_type:

training_status: TrainingStatus = Field(sa_type=Enum(TrainingStatus))

@bashtian-fr
Copy link

I prefre to use sa_type:

training_status: TrainingStatus = Field(sa_type=Enum(TrainingStatus))

this create the following error for me:

class NotificationPriority(Enum):
    LOW = "LOW"
    MEDIUM = "MEDIUM"
    HIGH = "HIGH"
    CRITICAL = "CRITICAL"
    
....    
File "/workspaces/api/src/n0tifs/models.py", line 255, in Notification
    priority: NotificationPriority = Field(sa_type=Enum(NotificationPriority))
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/sqlalchemy/sql/sqltypes.py", line 1397, in __init__
    self._enum_init(enums, kw)
  File "/home/vscode/.local/lib/python3.12/site-packages/sqlalchemy/sql/sqltypes.py", line 1427, in _enum_init
    self._default_length = length = max(len(x) for x in self.enums)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/sqlalchemy/sql/sqltypes.py", line 1427, in <genexpr>
    self._default_length = length = max(len(x) for x in self.enums)
                                        ^^^^^^
TypeError: object of type 'type' has no len()

@Mekacher-Anis
Copy link

Mekacher-Anis commented Oct 19, 2024

@bashtian-fr I have the same issue SQLModel version 0.0.21

@Tyskiep99
Copy link

Tyskiep99 commented Oct 20, 2024

@bashtian-fr I have the same issue SQLModel version 0.0.21

Hey there, I solved it by doing the following:

import enum
from sqlmodel import Column, Enum, Field

class NotificationPriority(str, enum.Enum):
    LOW = "LOW"
    MEDIUM = "MEDIUM"
    HIGH = "HIGH"
    CRITICAL = "CRITICAL"
    
class Notification(TableMixin, NotificationBase, table=True):
    id: int | None = Field(default=None, primary_key=True)
    priority: NotificationPriority = Field(sa_column=Column(Enum(NotificationPriority)))    

Note, that the enum class extend str AND Enum, which is from the enum module, while I use SQLModel Enum for the column

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