-
-
Notifications
You must be signed in to change notification settings - Fork 660
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
Improper UUID serialisation dropping leading zeros ▶️ "ValueError: badly formed hexadecimal UUID string" #25
Improper UUID serialisation dropping leading zeros ▶️ "ValueError: badly formed hexadecimal UUID string" #25
Comments
Rather than integer based serialization grandfathered in from sqlalchemy, use the stdlib [`UUID.hex`](https://docs.python.org/3/library/uuid.html#uuid.UUID.hex) method. This also fixes fastapi#25
I am also hitting this issue with UUIDs being "badly formed". |
I am also getting this issue. A new release with the PR merged will be very helpful. |
Using this fork until fastapi/sqlmodel#25 is fixed in the master and a new release is done
Using this fork until fastapi/sqlmodel#25 is fixed in the master and a new release is done `poetry add git+ssh://git@github.com:macrosfirst/sqlmodel.git#main`
For those awaiting a fix for this, here's a method to ensure the generated UUIDs don't start with leading zeros: def new_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 0
val = uuid.uuid4()
while val.hex[0] == '0':
val = uuid.uuid4()
return val
class UuidMixin(SQLModel):
# amend index / primary_key as needed
id: uuid.UUID = Field(index=False, primary_key=True, default_factory=new_uuid) |
Other solution: installing the version of @andrewbolster's PR:
|
Another (temporary) solution, with SQLAlchemy-Utils: from sqlalchemy_utils import UUIDType
from sqlmodel import Column, Field, SQLModel
from uuid import UUID, uuid4
class MyModel(SQLModel, table=True):
uuid: UUID = Field(default_factory=uuid4, sa_column=Column(UUIDType(), primary_key=True)) |
It fixes "Badly formed hexadecimal UUID string" bug (fastapi/sqlmodel#25)
@tiangolo is there any update on this? |
Thanks for the report and fix in #26 @andrewbolster! 🍰 And thanks everyone for the discussion. ☕ The fix will be available in SQLModel |
First Check
Commit to Help
Example Code
Cleansed Traceback
Description
Running through the tutorial up until around this point, I tried swapping out the
int
id key for aUUID
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 usingUUID().hex
,sqltypes.py
carries on sqlalchemy's integer stringification apprach, however, doesn't include any padding declaration.IMO there are two options;
UUID
int
str
approach with the padding fixed (f"{v.int:032x}"
)UUID.hex
implementation 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
The text was updated successfully, but these errors were encountered: