-
-
Notifications
You must be signed in to change notification settings - Fork 719
str typed attribute doesn't get value of Enum field #105
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
Comments
If you use this (below), does it work?
FileState inherits from the string class, so you do comparisons like |
Hey there! This might have been solved in #165, available since SQLModel 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. 🤓 |
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. |
I'm running into this as well. I made an example for this. from enum import StrEnum, auto
from typing import Annotated
from sqlmodel import Session, 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.
class AnEnum(StrEnum):
FIRST = auto()
SECOND = auto()
class Thing(SQLModel, table=True):
id: Annotated[int, Field(primary_key=True)]
an_enum: AnEnum
def test() -> None:
a_thing = Thing(id=1, an_enum=AnEnum.FIRST)
assert a_thing.an_enum == AnEnum.FIRST
assert isinstance(a_thing.an_enum, AnEnum)
# Postgres also appears to be affected.
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
assert a_thing.an_enum == AnEnum.FIRST
assert isinstance(a_thing.an_enum, AnEnum)
session.add(a_thing)
session.commit()
assert a_thing.an_enum == AnEnum.FIRST
# Right after commit, the type changes from AnEnum to str
assert isinstance(a_thing.an_enum, AnEnum)
same_thing_from_db = session.get(Thing, 1)
assert same_thing_from_db is not None
assert same_thing_from_db.an_enum == AnEnum.FIRST
assert isinstance(same_thing_from_db.an_enum, AnEnum)
print("Success!")
if __name__ == "__main__":
test() |
First Check
Commit to Help
Example Code
Description
state
attr value is not a string, but a FileState instanceOperating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9
Additional Context
No response
The text was updated successfully, but these errors were encountered: