On Apr 19, 2013, at 11:01 AM, James Hartley <jjhart...@gmail.com> wrote:

> On Wed, Apr 17, 2013 at 2:59 PM, Michael Bayer <mike...@zzzcomputing.com> 
> wrote:
>> James Hartley <jjhart...@gmail.com> writes:
>> > Is it possible to map Table instances back to classes defined through
>> > declarative_base()?
> 
> the typical form is:
> 
> Base = declarative_base()
> 
> some_table = Table('some_table', Base.metadata, Column('id', Integer, 
> primary_key=True))
> 
> class SomeClass(Base):
>    __table__ = some_table
> 
> Thanks all for the responses.
> 
> The Wiki recipe for views:
> 
> http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Views
> 
> ...creates & drops the defined view on-the-fly.  This raises two questions.  
> Is there a way to allow the view created in Python code to persist? 

when you emit "CREATE VIEW", which is what that recipe helps you do, that's a 
persistent view in the database.

> Likewise, is there a way to take advantage of an existing view defined at the 
> database level? 

Sure, you'd make a Table object, giving it the name of the view as well as the 
columns that the view returns.  Then you just SELECT from it normally.   This 
can also be accomplished via reflection, Table("myview", metadata, 
autoload=True, autoload_with=engine).


> An obvious workaround is to create a duplicate view with a different name, 
> but I'm curious as to whether the two can be merged.

for a more integrated approach, the "def view()" in the recipe can be enhanced 
to allow a "reflect from database" argument, or a list of columns, something 
like that.  It's a recipe so that it illustrates techniques which you can alter 
to suit your specific needs.


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


Reply via email to