-
First Check
Commit to Help
Example Codeimport datetime as dt
from enum import Enum
from sqlmodel import Field, SQLModel, Session, select
class FeatureType(str, Enum):
GEOSPATIAL = "geospatial"
TIME_GEOSPATIAL = "time-geospatial"
class Feature(SQLModel, table=True):
feature_id: str = Field(primary_key=True)
feature_name: str = Field(index=True, unique=True)
feature_type: FeatureType
with Session(engine) as session:
statement = select(Feature).where(Feature.feature_name == feature_name)
feature: Feature | None = session.exec(statement).first() DescriptionAfter upgrading SQLModel from version 0.0.8 to 0.0.10 I get the following error:
I am using an AWS Redshift database. The data (Feature object) I am trying to read was inserted and read correctly in version 0.0.8. It seems that when upgrading SQLAlchemy with SQLModel it is requiring the Enum type to exist in the DB instead of just in the Pydantic schema. If this were the case, this upgrade would require a mandatory update in the DDLs of each database table. It is possible that this issue is only related to SQLAlchemy. Operating SystemLinux Operating System DetailsRunning in a container Docker image python:3.11-slim-buster SQLModel Version0.0.10 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
I'm seeing this as well. The enum format seems to have changed, which is a pretty breaking change. Not too hard to update, but probably worth looking into some more. I'm seeing this when going from 0.0.8 to 0.0.9, 0.0.10, or 0.0.11. |
Beta Was this translation helpful? Give feedback.
-
I think this may be hidden now as the question is marked as answered. It seems like something that would be nice for someone else like @tiangolo to have a look at. |
Beta Was this translation helpful? Give feedback.
-
For those who are running into the same issue because your database column has The root cause is probably from Overriding the from sqlmodel import Column, Field, SQLModel
from sqlmodel.sql.sqltypes import AutoString
class Feature(SQLModel, table=True):
...
# If define sa_type
feature_type: FeatureType = Field(sa_type=AutoString) # or sqlalchemy.types.String
# If define sa_column
feature_type: FeatureType = Field(sa_column=Column(AutoString, ...other kwargs) Note: |
Beta Was this translation helpful? Give feedback.
-
Thanks @hohobilly. It works!!! |
Beta Was this translation helpful? Give feedback.
For those who are running into the same issue because your database column has
varchar
type instead of enum:The root cause is probably from
0.0.9
, a change in enum type checks ordering was introduced. It used to be checking String first and determine the type assqlalchemy.types.String
. After0.0.9
it returnssa_Enum(enum.Enum)
instead.Overriding the
sa_type
orsa_column
manually worked for me: