tuple_ returns string whereas I would expect tuple

Issue #4070 wontfix
Dmytro Starosud
created an issue

Hello team

Should I expect sqlalchemy.sql.expression.tuple_ to parse SQL result before returning to application code? I think that would be reasonable.

In [22]: db.query(tuple_(1,None,tuple_('Hello', 42),3,'')).scalar()
Out[22]: '(1,,"(Hello,42)",3,"")'

Comments (4)

  1. Michael Bayer repo owner

    It would be nice however there's no "tuple" datatype on the SQLAlchemy side right now and that string you are getting is something that psycopg2 (I assume this is postgresql / psycopg2 in use? always put that on a bug report :) ) is also not recognizing. If I look in https://github.com/psycopg/psycopg2/blob/master/psycopg/pgtypes.h it's not clear what type psycopg2 itself would be interpreting this as. This would be something you can bring up with the psycopg2 folks because I'm not exactly sure how this would work.

    one thought is to cast into ARRAY which would just "work" but I see your tuple has heterogeneous types in it (string and int) so that wouldn't work unless you could keep everything as a single type.

    If you want to just intercept this tuple() on your own you can make a custom type that does so (e.g. http://docs.sqlalchemy.org/en/latest/core/custom_types.html#types-custom ) but it would have the unenviable task of parsing out the strings and quotes and nested tuples inside the value. psycopg2 itself also would allow you to build a type interceptor of a similar nature but more transparently (might be nicer for something like this) , http://initd.org/psycopg/docs/advanced.html#type-casting-of-sql-types-into-python-objects.

    on the SQLAlchemy end I don't see a feasible way to get into this, psycopg2 driver support would be preferred and you should definitely ask on their mailing list if there's other ways to handle this.

  2. Log in to comment