- edited description
sqlalchemy.exc.UnsupportedCompilationError JSONB
Trying to compile query:
_update = str(S.__table__.update().where(and_(*_filters)).values({col.name: func.jsonb_concat(col,func.cast(bindparam(bind_name), JSONB))}))
and getting error:
sqlalchemy.exc.UnsupportedCompilationError: Compiler <sqlalchemy.sql.compiler.GenericTypeCompiler object at 0x108cd6110> can't render element of type <class 'sqlalchemy.dialects.postgresql.json.JSONB'>
if I remove cast then sqlalchemy trying to cast dict to HSTORE
Comments (17)
-
reporter -
repo owner - changed status to invalid
JSONB is a postgresql specific type so you need to compile with the PG dialect:
str(stmt.compile(dialect=postgresql.dialect()))
-
repo owner see
#3081 -
reporter ooops, sorry, really though thought sqlalchemy knows it from "S(Base)"
-
repo owner it will,
#3081will improve this at some point -
reporter btw, str(stmt.compile(dialect=postgresql.dialect())) returns different string, I always was getting params like ":b_state" now they all compiled and I see "%(b_state)s"
-
repo owner yes because that's the paramstyle accepted by the psycopg2 dialect.
-
reporter now I have to escape all params by myself? :) hope for something like passing them to execute(stmt, **params) :)
-
repo owner i dont see why, just connection.execute() the statement. the string format here is not necessary as something you need to access explicitly.
-
reporter Long story...I use statement where i can, but I'm using partitioning in PG and for update queries I'm generating string where I change table name
_update = str(S.__table__.update().where(and_(*_filters)).values(_values)).replace(S.__tablename__, '%(tablename)s'))
then I do:
execute(text(_update % {'tablename': tablename}), params)
couldn't made up a better solution.
-
repo owner See the recipe at https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/SessionModifiedSQL which illustrates using before_cursor_execute() along with connection.info to intercept table names and search and replace them.
-
reporter Thank you, definitely will try.
-
reporter can't make statement with returning
S.__table__.update().where(and_(*_filters)).values(_values).returning(S.id)
where should I put dialect name? Really can't find anything in docs :(
-
repo owner -
reporter Right now I'm not trying to render expression as string. I'm just trying to build update statement with "returning".
-
repo owner the returning() method as you have above
plenty of docs:
-
reporter I'm sorry, my mistake, copypasted code without buildng str from statement but was actually building it in my code. Sorry for stupid questions :)
- Log in to comment