0

I've the following construct which seems to produce the desired SQL:

>>> print session.query(exists('1').where(MyTable.name=='x'))
SELECT EXISTS (SELECT 1 
FROM my_table
WHERE my_table.name = :name_1) AS anon_1

However, when I try to execute it with .scalar() or .all() it returns the error:

*** UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session

How can I bind it for this simple query? I don't want to do bool(MyTable.query.filter(MyTable.name=='x').first()) as that wastefully pulls back the entire row from the table.


Update:

I've also tried:

>>> session.connection(mapper=MyTable).execute(
        exists('1').where(MyTable.name=='x'))
StatementError: Not an executable clause 'EXISTS \
  (SELECT 1 \nFROM my_table \nWHERE my_table.name = %(name_1)s)' []
EoghanM
  • 25,161
  • 23
  • 90
  • 123
  • You should have a line that reads `session.configure(bind=engine)` – sayap Mar 06 '13 at 01:50
  • `session.configure()` throws error `AttributeError: 'function' object has no attribute 'configure'` `session.__class__` is `sqlalchemy.orm.scoping.ScopedSession` – EoghanM Mar 06 '13 at 10:54
  • That doesn't sound right. Please add the relevant code where `session` is constructed and where `session.configure()` is run. – sayap Mar 06 '13 at 19:54
  • Hmmm I'm using TurboGears (1.5.1), investigating.. – EoghanM Mar 07 '13 at 08:11

1 Answers1

2

Got it I think:

>>> session.connection(mapper=MyTable).execute(
      select([exists('1').where(MyTable.name=='x')]))
EoghanM
  • 25,161
  • 23
  • 90
  • 123