Skip to content

✨ Update GUID handling to use stdlib UUID.hex instead of an int #26

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

Merged
merged 2 commits into from
Aug 27, 2022

Conversation

andrewbolster
Copy link
Contributor

Rather than integer based serialization grandfathered in from sqlalchemy, use the stdlib UUID.hex method.

This also fixes #25

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
@andrewbolster andrewbolster changed the title Update GUID handling use stdlib UUID.hex Update GUID handling to use stdlib UUID.hex Aug 26, 2021
@hidaris
Copy link

hidaris commented Oct 9, 2021

any update?

@gnillev
Copy link

gnillev commented Oct 15, 2021

I will just propose also adding a small test, just to catch this.

I made my own small test to catch the behaviour in a fork to fix this, before finding this pr, so you can maybe work something off of this (probably want to generalize a bit - I made it really quick just to confirm this was causing the issue):

def test_zero_padded_uuid():
    raised = None
    myid = UUID(hex='00000000000000000000000000000001')
    stored_value = GUID().process_bind_param(myid, SQLiteDialect)
    try:
        back_to_uuid = GUID().process_result_value(stored_value, SQLiteDialect)
    except ValueError as ex:
        raised = ex
    assert raised is None
    assert myid == back_to_uuid

RobertRosca added a commit to RobertRosca/sqlmodel that referenced this pull request Nov 2, 2021
@chriswhite199
Copy link
Contributor

@tiangolo - Do you have any other maintainers that can help get small PRs like this merged? I'm also up against this issue, and the PR was opened 2 months ago.

@mkarbo
Copy link

mkarbo commented Dec 2, 2021

Any news on this?

@alucarddelta
Copy link
Contributor

alucarddelta commented Dec 16, 2021

I am disappointed that this simple fix was not implemented in 0.0.5, as this is causes a lot of headaches when using UUID primary keys.

@mkarbo
Copy link

mkarbo commented Dec 29, 2021

We can hope for 0.0.7 to add this @tiangolo?

@pythrick
Copy link

I was just about to create an issue about it and implement the exactly same solution.

Looks like SQLModel got inspired by the same source of this gist: https://gist.github.com/gmolveau/7caeeefe637679005a7bb9ae1b5e421e

IMHO I think SQLAlquemy-Utils uses a better approach when it provides additional native dialects besides postgresql, like msql and cockroachdb, and also uses the native UUID .hex property in serialization. https://sqlalchemy-utils.readthedocs.io/en/latest/_modules/sqlalchemy_utils/types/uuid.html#UUIDType

synodriver pushed a commit to synodriver/sqlmodel that referenced this pull request Jun 24, 2022
@codecov
Copy link

codecov bot commented Aug 27, 2022

Codecov Report

Merging #26 (9bb4715) into main (db3ad59) will not change coverage.
The diff coverage is 0.00%.

@@           Coverage Diff           @@
##             main      #26   +/-   ##
=======================================
  Coverage   97.59%   97.59%           
=======================================
  Files         182      182           
  Lines        6060     6060           
=======================================
  Hits         5914     5914           
  Misses        146      146           
Impacted Files Coverage Δ
sqlmodel/sql/sqltypes.py 56.75% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@tiangolo tiangolo changed the title Update GUID handling to use stdlib UUID.hex ✨ Update GUID handling to use stdlib UUID.hex instead of an int Aug 27, 2022
@github-actions
Copy link

📝 Docs preview for commit 9bb4715 at: https://630a99822de66e5008e74d00--sqlmodel.netlify.app

@tiangolo
Copy link
Member

Great! This makes sense, thank you @andrewbolster! 🍰

And thanks everyone for the discussion! ☕

This will be available in the next version, released in the next hours, SQLModel 0.0.7. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improper UUID serialisation dropping leading zeros ▶️ "ValueError: badly formed hexadecimal UUID string"
8 participants