How can I bind a list to a parameter in a custom query in SQLAlchemy?

A new approach to this problem that works for any database (not just relying on psycopg2’s type adaptation) uses expanding bind parameters:

sql_tmpl = """delete from Data where id_data in :iddata"""
params = { 'iddata': [1, 2, 3, 4], }
# session is a session object from sqlalchemy
t = text(sql_tmpl)
t = t.bindparams(bindparam('iddata', expanding=True))
self.session.execute(t, params)

Leave a Comment