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
For those awaiting a fix for this, here's a method to ensure the generated UUIDs don't start with leading zeros:
defnew_uuid() ->uuid.UUID:
# Note: Work around UUIDs with leading zeros: https://github.com/tiangolo/sqlmodel/issues/25# by making sure uuid str does not start with a leading 0val=uuid.uuid4()
whileval.hex[0] =='0':
val=uuid.uuid4()
returnvalclassUuidMixin(SQLModel):
# amend index / primary_key as neededid: uuid.UUID=Field(index=False, primary_key=True, default_factory=new_uuid)
Activity
Update GUID handling use stdlib UUID.hex
alucarddelta commented on Sep 1, 2021
I am also hitting this issue with UUIDs being "badly formed".
dit7ya commented on Sep 27, 2021
I am also getting this issue. A new release with the PR merged will be very helpful.
Use a fork of SQLModel
Use a fork of SQLModel
chriswhite199 commented on Nov 29, 2021
For those awaiting a fix for this, here's a method to ensure the generated UUIDs don't start with leading zeros:
frankie567 commented on Nov 30, 2021
Other solution: installing the version of @andrewbolster's PR:
junekiwi commented on Dec 20, 2021
Another (temporary) solution, with SQLAlchemy-Utils:
Use my own fork of SQLModel 0.0.6 merged with andrewbolster's fork
mkarbo commented on Mar 16, 2022
@tiangolo is there any update on this?
fix: work around for fastapi/sqlmodel#25
Merge pull request fastapi#25 from leynier/fork-3
tiangolo commented on Aug 27, 2022
Thanks for the report and fix in #26 @andrewbolster! 🍰
And thanks everyone for the discussion. ☕
The fix will be available in SQLModel 0.0.7, released in the next hours. 🎉
Increase sqlmodel version to ^0.0.7