View support

Issue #181 resolved
Former user created an issue

(original reporter: Justin) Right now a common way to get more performance out of an ORM is to create a view for one or many tables and map objects to that. SQLAlchemy could have a way to define views in a similar way as tables. Additionally, for backends that do not support views, it could emulate them by using subselects.

so you could do something like

myview = View('myview', "select street,count(street) as count from addresses group by street")
myviewtable = Table(myview, db,
   Column('street', String),
   Column('count', Integer),
)

Plain text queries as well sql.Select objects should be supported.

Currently, SQLAlchemy lets you get away with this

q='(select street,count(street) as count from addresses group by street)'

ctable=Table(q, db,
    Column('street', String),
    Column('count', Integer),
).alias("counts")
class Counts(object):
    pass
assign_mapper(Counts,ctable,primary_key=[ctable.c.street](ctable.c.street))
for x in Counts.select():
    print x.street, x.count

Comments (1)

  1. Mike Bayer repo owner

    you can do this with a plain select() and the text case can be like:

    select([column('y'), column('z')](column('x'),)).where("foo=bar and lala=hoho")
    
  2. Log in to comment