Session.get_bind can't handle **kwargs sent by Session.execute

Issue #1558 resolved
Former user created an issue

If you create sqlalchemy.sql.text instance with parameters to bind e.g:

import sqlalchemy.sql as sql
t = sql.text('select * from test where foo = :foo')

and then try to execute query like this:

from sqlalchemy import Table, Column, MetaData, Unicode, create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
meta = MetaData()

table = Table('test', meta, Column('foo', Unicode)
)

engine = engine = create_engine('postgres://user:@localhost:5432/test', echo = True)
Session = scoped_session(sessionmaker())
Session.configure(bind = engine)
meta.bind = engine
meta.drop_all()
meta.create_all()

Session.execute(t, foo = u'bar')

... it won't work:

Traceback (most recent call last):
  File "./test.py", line 19, in <module>
    Session.execute(t, foo = u'bar')
  File "/dev/env/lib/python2.5/site-packages/SQLAlchemy-0.5.6-py2.5.egg/sqlalchemy/orm/scoping.py", line 123, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/dev/env/lib/python2.5/site-packages/SQLAlchemy-0.5.6-py2.5.egg/sqlalchemy/orm/session.py", line 752, in execute
    engine = self.get_bind(mapper, clause=clause, **kw)
TypeError: get_bind() got an unexpected keyword argument 'foo'

That's because Session.execute do this:

engine = self.get_bind(mapper, clause=clause, **kw)

and Session.get_bind has following signature:

def get_bind(self, mapper, clause=None):

and can't handle that call with extra foo argument.

On the other hand this will work:

Session.connection().execute(t, foo = u'bar')

Comments (1)

  1. Mike Bayer repo owner

    its an unfortunate product of the interface, but the bind params are sent as follows:

    Session.execute(stmt, params={...})
    
  2. Log in to comment