On Thu, Oct 8, 2020 at 3:38 AM Richard Damon <[email protected]> wrote:
>
> I am working on a app using SQLAlchemy's ORM layer to interface to the
> database, but I am running into an issue that if an object has
> 'complicated' property, like a UUID, that SQLAlchemy doesn't know how to
> handle. One option would be to make the propery actually only hold a
> database suitable representation, and I see an option to add a
> 'reconstructor' to convert a value read from the database into an
> object. Is there a similar way that when writing the object, to indicate
> how to convert the object into a format that can be put into the database?
>

The usual pattern is to create a custom datatype using
sqlalchemy.types.TypeDecorator. You would override the
process_bind_baram and process_result_value methods to convert objects
between the python and database representations:

https://docs.sqlalchemy.org/en/13/core/custom_types.html#augmenting-existing-types

For the specific case of UUIDs, there is an example in the docs that
will use Postgres' UUID type against postgres, and CHAR against other
databases:

https://docs.sqlalchemy.org/en/13/core/custom_types.html#backend-agnostic-guid-type

Hope that helps,

Simon

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeVbT%3DAN%2BBTxNVoMY0PY3U7rTK73d1XHcbZYM%2BkQpp1RA%40mail.gmail.com.

Reply via email to