First Check
Commit to Help
Example Code
importuuidfromtypingimportOptionalimportsqlalchemy.excfromsqlmodelimportSQLModel, Fieldfromsqlmodelimportcreate_engine, Session, selectclassHero(SQLModel, table=True):
id: Optional[uuid.UUID] =Field(primary_key=True,
default_factory=uuid.uuid4)
name: strsecret_name: strage: Optional[int] =Nonedef__str__(self):
returnf'{self.name} (AKA {self.secret_name}) [{self.ageifself.ageisnotNoneelse"Unknown"}]'sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url, echo=True) # remove echo for proddefcreate_db_and_tables():
try:
Hero.__table__.drop(engine)
exceptsqlalchemy.exc.OperationalError: # table doesn't exist yetpassSQLModel.metadata.create_all(engine)
defcreate_heroes():
heros= [
Hero(name="Deadpond", secret_name="Dive Wilson"),
Hero(name="Spider-Boy", secret_name="Pedro Parqueador"),
Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48),
Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32),
Hero(name="Black Lion", secret_name="Trevor Challa", age=35),
Hero(name="Dr. Weird", secret_name="Steve Weird", age=36),
Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93),
]
withSession(engine) assession:
forheroinheros:
session.add(hero)
session.commit()
defselect_heroes():
withSession(engine) assession:
forheroinsession.exec(select(Hero)):
print(hero.id)
if__name__=="__main__":
whileTrue:
try:
create_db_and_tables()
create_heroes()
select_heroes()
exceptValueError:
raiseCleansed Traceback
Traceback (most recent call last):
File “/…/sqlmodel_tutorial/minimal_example.py", line 89, in <module>
select_heroes()
File “/…/sqlmodel_tutorial/minimal_example.py", line 80, in select_heroes
for hero in session.exec(select(Hero)):
File "/.../lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 381, in iterrows
for row in self._fetchiter_impl():
File "/.../lib/python3.8/site-packages/sqlalchemy/orm/loading.py", line 147, in chunks
fetch = cursor._raw_all_rows()
File "/.../lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 392, in _raw_all_rows
return [make_row(row) for row in rows]
File "/.../lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 392, in <listcomp>
return [make_row(row) for row in rows]
File "/.../lib/python3.8/site-packages/sqlalchemy/sql/type_api.py", line 1430, in process
return process_value(value, dialect)
File "/.../lib/python3.8/site-packages/sqlmodel/sql/sqltypes.py", line 61, in process_result_value
value = uuid.UUID(value)
File “/…/lib/python3.8/uuid.py", line 171, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
Description
Running through the tutorial up until around this point, I tried swapping out the int id key for a UUID key.
This works except whenever the uuid.uuid4 default factory creates a UUID with a hexstring that starts with a zero.
It appears that here in sqltypes.py that, rather than using UUID().hex, sqltypes.py carries on sqlalchemy's integer stringification apprach, however, doesn't include any padding declaration.
IMO there are two options;
- faithfully fix the replication of SQLAlchemy's
UUID▶️int▶️str approach with the padding fixed (f"{v.int:032x}") - switch to using the stdlib
UUID.hex implementation for serialisation
Personally I'm a fan of the latter so will spin up a PR
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.8.8
Additional Context
No response
First Check
Commit to Help
Example Code
Cleansed Traceback
Description
Running through the tutorial up until around this point, I tried swapping out the
intid key for aUUIDkey.This works except whenever the
uuid.uuid4default factory creates a UUID with a hexstring that starts with a zero.It appears that here in
sqltypes.pythat, rather than usingUUID().hex,sqltypes.pycarries on sqlalchemy's integer stringification apprach, however, doesn't include any padding declaration.IMO there are two options;
UUIDintstrapproach with the padding fixed (f"{v.int:032x}")UUID.heximplementation for serialisationPersonally I'm a fan of the latter so will spin up a PR
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.8.8
Additional Context
No response