I'm trying to run this simple raw sql statement with parameters with SQLALchemy (within an alembic script) :
from alembic import op
t = {"code": "123", "description": "one two three"}
op.execute("insert into field_tags (id, field_id, code, description) "+
"values (1,'zasz', :code ,:description')", t)
And I get the following error :
sqlalchemy.exc.StatementError: A value is required for bind parameter
'description' (original cause: InvalidRequestError: A value is required for
bind parameter 'description') "insert into field_tags (id, field_id, code,
description) values (1, 'math',
%(code)s ,%(description)s)" []
The solution:
t = {"code": "123", "description": "one two three"}
from sqlalchemy.sql import text
op.get_bind().execute(text("insert into field_tags (id, field_id, code, description) "+
"values (1,'zasz', :code ,:description')"), **t)
question from:
https://stackoverflow.com/questions/23206562/sqlalchemy-executing-raw-sql-with-parameter-bindings 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…