Wrong `bindparam()` call signature in "Specifying Bound Parameter Behaviors"

Issue #4220 resolved
Ilja Everilä
created an issue

In "Specifying Bound Parameter Behaviors" there is:

The parameters can also be explicitly typed:

stmt = stmt.bindparams(bindparam("x", String), bindparam("y", String))

but the call signature of bindparam() is:

sqlalchemy.sql.expression.bindparam(key, value=symbol('NO_ARG'), type_=None, ...)

Hence the example in the documentation would end up passing the type as the value argument. The error is not apparent given the example from the documentation, since the values are overridden when executing the statement, but will become apparent, if not:

>>> stmt = text("SELECT * FROM users WHERE users.name BETWEEN :x AND :y")
>>> stmt = stmt.bindparams(bindparam("x", String), bindparam("y", String))
>>> result = conn.execute(stmt)
Traceback (most recent call last):
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/home/u/Work/sqlalchemy/lib/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type. [SQL: 'SELECT * FROM users WHERE users.name BETWEEN ? AND ?'] [parameters: (<class 'sqlalchemy.sql.sqltypes.String'>, <class 'sqlalchemy.sql.sqltypes.String'>)] (Background on this error at: http://sqlalche.me/e/rvf5)

The example should be:

stmt = stmt.bindparams(bindparam("x", type_=String), bindparam("y", type_=String))

Comments (4)

  1. Log in to comment