7

I'm not asking abount how to execute a raw sql string with sqlalchemy. I want to build sqlalchemy.orm.query.Query object which also can be built by code like below.

query = s.query(User).filter(User.age<10)

I tried something like

query = s.query('* from User where age<10')

It seems to work properly in some cases, but it's actual behavior is quite different from what I expected. For example, query.column_descriptions return wrong values. Speaking differently, I'm looking for a function which parses a sql string and returns a Query object which can be used to inspect the details. I expect there exists a simple way to do it. Does anyone know about this ?

4
  • Is s your session object? Commented Jul 13, 2016 at 15:59
  • Yes. It's a session object. Commented Jul 14, 2016 at 0:05
  • 3
    such thing doesn't exist. You can probably find few attempts / parsers that deal with simple conversions, e.g. wiggy.net/articles/sqlalchemy-in-reverse but don't expect to see a full-blown tool that will do the job for you. Otherwise we'd be using here on Stack Overflow =) Commented Jul 14, 2016 at 6:07
  • SQLAlchemy won't parse your textual SQL, but you yourself can annotate it with for example result column behaviour. See "Using Textual SQL". Commented Jul 14, 2016 at 7:47

0

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.