sqlalchemy.exc.UnsupportedCompilationError JSONB

Issue #3549 invalid
Roman D. created an issue

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)

  1. Mike Bayer repo owner

    JSONB is a postgresql specific type so you need to compile with the PG dialect:

    str(stmt.compile(dialect=postgresql.dialect()))

  2. Roman D. 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"

  3. Roman D. reporter

    now I have to escape all params by myself? :) hope for something like passing them to execute(stmt, **params) :)

  4. Mike Bayer 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.

  5. Roman D. 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.

  6. Roman D. 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 :(

  7. Roman D. reporter

    Right now I'm not trying to render expression as string. I'm just trying to build update statement with "returning".

  8. Roman D. 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 :)

  9. Log in to comment