You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
str typed attribute doesn't get value of Enum field #105
class FileState(str, enum.Enum):
new = "New"
validated = "Validated"
class File(SQLModel, table=True):
name: str = Field(index=True)
state: FileState = Field(
sa_column=Column(
Enum(FileState),
default=None,
nullable=True,
index=True
)
)
FileState inherits from the string class, so you do comparisons like assert result.state == "New" without throwing an exception (although that doesn't help with enum validation in your IDE). Validation in your IDE, mypy, etc. comes from setting the state type hint to FileState.
Hey there! This might have been solved in #165, available since SQLModel 0.0.7.
I tried running your example code but it seems it's missing imports and variables so I can't run it. If you're still having issues, please create a self-contained example that I can run to see your exact problem. 🤓
I'm running into this as well. I made an example for this.
fromenumimportStrEnum, autofromtypingimportAnnotatedfromsqlmodelimportSession, SQLModel, Field, create_engine# str, Enum is also affected.# Enum seems to work fine, however there's many reasons to use str, Enum or StrEnum.classAnEnum(StrEnum):
FIRST=auto()
SECOND=auto()
classThing(SQLModel, table=True):
id: Annotated[int, Field(primary_key=True)]
an_enum: AnEnumdeftest() ->None:
a_thing=Thing(id=1, an_enum=AnEnum.FIRST)
asserta_thing.an_enum==AnEnum.FIRSTassertisinstance(a_thing.an_enum, AnEnum)
# Postgres also appears to be affected.engine=create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
withSession(engine) assession:
asserta_thing.an_enum==AnEnum.FIRSTassertisinstance(a_thing.an_enum, AnEnum)
session.add(a_thing)
session.commit()
asserta_thing.an_enum==AnEnum.FIRST# Right after commit, the type changes from AnEnum to strassertisinstance(a_thing.an_enum, AnEnum)
same_thing_from_db=session.get(Thing, 1)
assertsame_thing_from_dbisnotNoneassertsame_thing_from_db.an_enum==AnEnum.FIRSTassertisinstance(same_thing_from_db.an_enum, AnEnum)
print("Success!")
if__name__=="__main__":
test()
Activity
fletcheaston commentedon Sep 24, 2021
If you use this (below), does it work?
FileState inherits from the string class, so you do comparisons like
assert result.state == "New"without throwing an exception (although that doesn't help with enum validation in your IDE). Validation in your IDE, mypy, etc. comes from setting the state type hint to FileState.tiangolo commentedon Aug 30, 2022
Hey there! This might have been solved in #165, available since SQLModel
0.0.7.I tried running your example code but it seems it's missing imports and variables so I can't run it. If you're still having issues, please create a self-contained example that I can run to see your exact problem. 🤓
github-actions commentedon Sep 10, 2022
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.
gitpushdashf commentedon Apr 28, 2023
I'm running into this as well. I made an example for this.