On 7/29/15 11:30 AM, Morgan McClure wrote:

I’ve been looking to make a simple typeadapter based on the HSTORE data type in postgres to do some value coercion and I have been having some problems.


I want to coerce the individual values inside my dictionary before they are put into the postgres HSTORE column and when I select keys, I want to coerce them back.


What methods do I need to subclass to intercept subkey/indexing operations as well as full assignment operations?


Normally for a simpler datatype I’d just implement process_bind_param, process_result_value but that hasn't been working the way I expect it to with HSTORE.


If it makes the problem simpler I don’t need the MutableExtension to work so I only need to coerce data on the first assignment to the column.


I'll primarily be selecting data like
    Query(MyObjectClass.MyHSTOREType['somekey'])

but occasionally, I may be doing

    Query(MyObjectClass.MyHSTOREType)

and would like my postgres->python coercion to work in both scenarios.


the issue here is that for PG's special types like HSTORE, JSON, ARRAY, JSONB, those objects are of those types sure, but when we use the special "index" operators, we *dont* get that type back; e.g. for an HSTORE, myobject['foo'] returns an expression that is of type Text:

>>> from sqlalchemy.dialects import postgresql
>>> from sqlalchemy import column
>>> print column('x', postgresql.HSTORE())['foo'].type
TEXT

The issue of these return types for the PG indexing types has been coming up a lot (https://bitbucket.org/zzzeek/sqlalchemy/issues/3499/flip-off-the-hashable-flag-for-pg, https://bitbucket.org/zzzeek/sqlalchemy/issues/3487/support-direct-mult-dimensional-array). Something will have to improve in general for this kind of issue.

For now you can apply your type using either cast() or coerce_type:

from sqlalchemy import coerce_type

Query(coerce_type(MyObjectClass.MyHSTOREType['somekey'], MySpecialType))

https://bitbucket.org/zzzeek/sqlalchemy/issues/3503/add-full-control-for-return-type-under-pg is added as a catchall to look into this.







Thanks in advance

--
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 <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to