You can do this, but you must define the primary key manually. Assuming id is the v column that you want to use as the primary key (as is the case with the original code example ), this works:
from sqlalchemy import orm class ViewName(object): def __init__(self, name): self.name = name orm.mapper(ViewName, v, primary_key=[vcid]) Session = orm.sessionmaker(bind=engine) for r in Session().query(ViewName): print r.id, r.number
To test this, simply insert this snippet at the end of my working example in the answer above. See the documentation for more details (for example, you can use properties to define foreign keys).
EDIT (van in comment to my answer above): Alternatively, you can slightly modify the view in my source code (and question) and write:
v = Table('viewname', metadata, Column('id', Integer, primary_key=True), autoload=True)
ie, add the primary key to the table definition already. Then you don't need the primary_key argument in orm.mapper , and the rest of the code in your question works out of the box.
stephan
source share