2

I want to do the select statement in sqlachemy like

select user.id from user where user.name = 'test'

I loaded user table by:

User = Table('user',metadata,autoload=True)
Result = session.query(User.c.id).filter_by(name='test').first()

I got an error

"NoInspectionAvailable: No inspection system is available for object of type "

Can you suggest how to carry out that SQL in sqlachemy ?

SieuTruc
  • 475
  • 1
  • 7
  • 16
  • 2
    Why `User.c.id` and not `User.id`? – fedorqui Dec 19 '13 at 15:51
  • 1
    @fedorqui if it was User.id, i'd get "'Table' object has no attribute 'id'" – SieuTruc Dec 19 '13 at 16:09
  • 1
    @alKid i don't want to create a class for mapping into, just want to use Table object. And if you take more carefully in the link you give me, there is no reference of using query and filter clauses together. – SieuTruc Dec 19 '13 at 16:10

1 Answers1

1

Solved:

r =session.query(statSoftware.c.id).filter(statSoftware.c.name=='Apache').first()

Using filter() instead of filter_by(), Allows to get all needed columns .

Kobi K
  • 7,743
  • 6
  • 42
  • 86
SieuTruc
  • 475
  • 1
  • 7
  • 16
  • Look [here](http://stackoverflow.com/a/2128558/1982962) to understand the differences. – Kobi K Dec 19 '13 at 16:21
  • i've seen it, but filter_by(name='test') doesn't work in this case. can you suggest how to use filter_by in my case ? – SieuTruc Dec 19 '13 at 16:25
  • There's no easy way to do this since you specified `statSoftware.c.id`. When you specify `filter_by`, it looks at the last entity specified by a `query` or `join` function. You specified a column, however, not a table in your `query`. SQLAlchemy 0.9 (not yet officially released) has [`load_only`](http://docs.sqlalchemy.org/en/latest/orm/mapper_config.html#load-only-cols), which could make this easier. But really, I'd still just recommend using `filter`. – Mark Hildreth Dec 19 '13 at 20:44