Re: [sqlalchemy] ORM and objects with properties that need conversion to write to database

2020-10-08 Thread Richard Damon
Thank you, that looks like what I was looking for but didn't know what
it was called.


On 10/8/20 4:55 AM, Simon King wrote:
> On Thu, Oct 8, 2020 at 3:38 AM Richard Damon  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
>

-- 
Richard Damon

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/9a984aed-26d1-5c13-d1b5-05529e58b2db%40Damon-Family.org.


Re: [sqlalchemy] ORM and objects with properties that need conversion to write to database

2020-10-08 Thread Simon King
On Thu, Oct 8, 2020 at 3:38 AM Richard Damon  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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeVbT%3DAN%2BBTxNVoMY0PY3U7rTK73d1XHcbZYM%2BkQpp1RA%40mail.gmail.com.


[sqlalchemy] ORM and objects with properties that need conversion to write to database

2020-10-07 Thread Richard Damon
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?

-- 
Richard Damon

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/a67e28b7-cd9e-174d-2f6e-cd38c9122f7d%40Damon-Family.org.