On Jun 9, 2011, at 12:41 AM, Reece Hart wrote:

> 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://pub...@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()
>        PKNotFoundError: table 'pseqalias' does not have a primary key 
> defined...

You would need to pass the columns to be considered part of the primary key to 
the underlying mapper, using sqlsoup.map_to(), but unfortunately there is not a 
simple interface for that at the moment, since you need the Table object as 
well to get at the columns.   So until this interface could be improved, for 
now it would look like:

metadata = u._metadata
t = Table("pseqaliases", metadata, autoload=True)

u.map_to("pseqaliases", selectable=t, mapper_args={"primary_key":[t.c.col1, 
t.c.col2]})

This is just the "primary_key" argument to mapper, there are some examples at 
http://www.sqlalchemy.org/docs/orm/mapper_config.html near the top. 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to