Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> See also the SQLAlchemy usage recipe <a href="https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/Views" rel="noreferrer">here</a></p> <p>Creating a (read-only non-materialized) view is not supported out of the box as far as I know. But adding this functionality in SQLAlchemy 0.7 is straightforward (similar to the example I gave <a href="https://stackoverflow.com/a/9597404/92092">here</a>). You just have to write a <a href="http://docs.sqlalchemy.org/en/latest/core/compiler.html" rel="noreferrer">compiler extension</a> <code>CreateView</code>. With this extension, you can then write (assuming that <code>t</code> is a table object with a column <code>id</code>)</p> <pre><code>createview = CreateView('viewname', t.select().where(t.c.id&gt;5)) engine.execute(createview) v = Table('viewname', metadata, autoload=True) for r in engine.execute(v.select()): print r </code></pre> <p>Here is a working example:</p> <pre><code>from sqlalchemy import Table from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql.expression import Executable, ClauseElement class CreateView(Executable, ClauseElement): def __init__(self, name, select): self.name = name self.select = select @compiles(CreateView) def visit_create_view(element, compiler, **kw): return "CREATE VIEW %s AS %s" % ( element.name, compiler.process(element.select, literal_binds=True) ) # test data from sqlalchemy import MetaData, Column, Integer from sqlalchemy.engine import create_engine engine = create_engine('sqlite://') metadata = MetaData(engine) t = Table('t', metadata, Column('id', Integer, primary_key=True), Column('number', Integer)) t.create() engine.execute(t.insert().values(id=1, number=3)) engine.execute(t.insert().values(id=9, number=-3)) # create view createview = CreateView('viewname', t.select().where(t.c.id&gt;5)) engine.execute(createview) # reflect view and print result v = Table('viewname', metadata, autoload=True) for r in engine.execute(v.select()): print r </code></pre> <p>If you want, you can also specialize for a dialect, e.g.</p> <pre><code>@compiles(CreateView, 'sqlite') def visit_create_view(element, compiler, **kw): return "CREATE VIEW IF NOT EXISTS %s AS %s" % ( element.name, compiler.process(element.select, literal_binds=True) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload