1

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)' []
4
  • You should have a line that reads session.configure(bind=engine) Commented Mar 6, 2013 at 1:50
  • session.configure() throws error AttributeError: 'function' object has no attribute 'configure' session.__class__ is sqlalchemy.orm.scoping.ScopedSession Commented Mar 6, 2013 at 10:54
  • That doesn't sound right. Please add the relevant code where session is constructed and where session.configure() is run. Commented Mar 6, 2013 at 19:54
  • Hmmm I'm using TurboGears (1.5.1), investigating.. Commented Mar 7, 2013 at 8:11

1 Answer 1

3

Got it I think:

>>> session.connection(mapper=MyTable).execute(
      select([exists('1').where(MyTable.name=='x')]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.