I’d like to use SqlSoup with an existing database that contains views. Accessing a table goes swimmingly, but accessing a view results in “PKNotFoundError: table ‘[viewname]’ does not have a primary key defined…”
Do I correctly infer that SqlSoup does not work with database views (by default, at least)? I’ve been unable to find anything directly relevant on Google, SO, or the SqlAlchemy mailing list. If you were faced with this, how would you proceed if you wanted to access non-updatable views? I’m new to SQLAlchemy and SQLSoup.
Here’s a specific example:
from sqlalchemy.ext.sqlsoup import SqlSoup
u = SqlSoup('postgresql+psycopg2://PUBLIC@unison-db.org:5432/unison')
seq = u.pseq.filter(u.pseq.pseq_id==76).all() # okay
aliases = u.pseqalias.filter(u.pseqalias.pseq_id==76).all()
This is a public database. You can run the equivalent queries using psql:
psql -h unison-db.org -U PUBLIC -d unison -c 'select * from pseq where pseq_id=76'
psql -h unison-db.org -U PUBLIC -d unison -c 'select * from pseqalias where pseq_id=76'
Thanks to Randy for the map() tip. Here’s a complete solution that you may try verbatim (the database is publicly available):
This is with Python 2.7.1, Alchemy 0.7.2.
For references, see: