why does sqlalchemy UUID succeed with mysql BINARY(16) but fails with postgres BYTEA

Skip to first unread message

Sam S

Oct 10, 2023, 8:35:18 PM10/10/23
to sqlalchemy
I noticed that mysql's BINARY(16) datatype can get read into sqlalchemy's UUID() no problem, but if it is postgres' BYTEA datatype, then it fails. I created a reproducible example at this gist: 

If someone could help me, I would like to know how i can read a binary UUID into model in postgres using sqlalchemy.

Thank you

Mike Bayer

Oct 11, 2023, 9:00:45 AM10/11/23
to noreply-spamdigest via sqlalchemy
PostgreSQL has a native UUID datatype, so when you use SQLAlchemy's UUID, it maps to a real PG UUID datatype, not BYTEA.

as for sqlalchemy_utils.UUIDType, we dont maintain that package here so you'd need to look at their source code.

Overall if you want complete "UUID mapped to any arbitrary binary datatype on any platform unambiguously", code it yourself, using the example at https://docs.sqlalchemy.org/en/20/core/custom_types.html#backend-agnostic-guid-type as a guide.  that example currently uses CHAR(32), but you can change it to use BYTEA/BINARY with appropriate changes.   sqlalchemy_utils likely got their UUID type from an older version of this example.
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
0 new messages