6

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 ?

12412316
  • 725
  • 7
  • 17
  • Is s your session object? – Bill Brower Jul 13 '16 at 15:59
  • Yes. It's a session object. – 12412316 Jul 14 '16 at 00:05
  • 2
    such thing doesn't exist. You can probably find few attempts / parsers that deal with simple conversions, e.g. http://www.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 =) – MOCKBA Jul 14 '16 at 06:07
  • SQLAlchemy won't parse your textual SQL, but you yourself can annotate it with for example result column behaviour. See ["Using Textual SQL"](http://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-textual-sql). – Ilja Everilä Jul 14 '16 at 07:47

0 Answers0