Search code examples
mysqlsqlalchemycasegroup-concat

SQLAlchemy: group_concat(case when...) possible?


I am trying to translate a MySQL query that uses group_concat in combination with case when to SQLAlchemy:

MySQL: group_concat(case when TableA.columnX=1 then TableA.id end)

I already tried queries like

func.group_concat(case([tableA.columnX], value=tableA.id)).label('tableA_id_concat'),

however this does not work (NotImplementedError). Is there a way to use group_concat in combination with case when in SQLAlchemy?

Note that TableA.columnX is a SQLAlchemy Boolean().


Solution

  • I found the problem, it was the case. The correct call is:

    func.group_concat(case(
                          [ 
                              (TableA.columnX, TableA.id) 
                          ]
                     )
    ).label('TableA_id_concat'),