databases icon indicating copy to clipboard operation
databases copied to clipboard

Sqlalchemy core columns with labels are compiled without database-specific dialects

Open snazzyfox opened this issue 4 years ago • 0 comments

from databases import Database
from sqlalchemy import sql
from sqlalchemy.dialects.postgresql import array

database = Database('postgresql://localhost/example')

# This works
# SQL Query is: SELECT ARRAY['a', 'b', 'c']
query = sql.select([
    array(['a', 'b', 'c'])
])
value = await database.fetch_val(query=query)  # return ['a', 'b', 'c']

# This breaks
# SQL Query is: SELECT ARRAY['a', 'b', 'c'] AS my_array
query = sql.select([
    array(['a', 'b', 'c']).label('my_array')
])
value = await database.fetch_val(query=query)

The second case raises this exception:

sqlalchemy.exc.UnsupportedCompilationError: Compiler <sqlalchemy.sql.compiler.StrSQLCompiler object at 0x7f35fa30c7d0> can't render element of type <class 'sqlalchemy.dialects.postgresql.array.array'> (Background on this error at: http://sqlalche.me/e/13/l7de)

This issue seems to appear whenever there is a postgres-specific construct inside of a labeled column.

snazzyfox avatar Feb 18 '21 21:02 snazzyfox