op() is old school. make a custom expression element:
from sqlalchemy.ext import compiler
from sqlalchemy.sql import ColumnElement
class group_concat(ColumnElement):
def __init__(self, col1, col2):
self.col1 = col1
self.col2 = col2
self.type = col1.type
@compiler.compiles(group_concat, 'mysql')
def compile_group_concat(element, compiler, **kw):
return "GROUP CONCAT(%s ORDER BY %s)" % (
compiler.process(element.col1),
compiler.process(element.col2)
)
from sqlalchemy import *
m = MetaData()
t = Table('t1', m, Column('foo', String), Column('bar', String))
print select([group_concat(t.c.foo,
t.c.bar)]).compile(bind=create_engine('mysql://'))
"SELECT GROUP CONCAT(t1.foo ORDER BY t1.bar) AS anon_1"
> >
>