views:

34

answers:

1

I know how to query on a model now. Suppose there is a Question model:

class Question(Base):
    __tablename__ = "questions"
    id=Column(...)
    user_id=Column(...)
    ...

Now, I can do:

question = Session.query(Question).filter_by(user_id=123).one()

But, now, I have a table (not a model) questions:

questions = Table('questions', Base.metadata,
                  Column(id, ...),
                  Column(user_id, ...),
                  ....)

How to query it as what I do with models?

Session.query(questions).filter_by(user_id=123).one()

This will report an error:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\Python27\lib\site-packages\sqlalchemy-0.6.3-py2.7.egg\sqlalchemy\orm\query.py", line 851, in filter_by
  for key, value in kwargs.iteritems()]
File "E:\Python27\lib\site-packages\sqlalchemy-0.6.3-py2.7.egg\sqlalchemy\orm\util.py", line 567, in _entity_descriptor
    desc = entity.class_manager[key]
AttributeError: 'NoneType' object has no attribute 'class_manager'

But:

Session.query(questions).all()

is OK.

Is filter_by only work for models? How can I query on tables?

+2  A: 

I think it's Session.query(questions).filter(questions.c.user_id==123).one()

THC4k