Re: [sqlalchemy] views & declarative?
On Apr 19, 2013, at 11:01 AM, James Hartley wrote: > On Wed, Apr 17, 2013 at 2:59 PM, Michael Bayer > wrote: >> James Hartley 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.
Re: [sqlalchemy] views & declarative?
On Wed, Apr 17, 2013 at 2:59 PM, Michael Bayer wrote: > James Hartley 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? Likewise, is there a way to take advantage of an existing view defined at the database level? 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. Thanks again for the insight shared. -- 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.
Re: [sqlalchemy] views & declarative?
On Apr 17, 2013, at 9:59 AM, James Hartley wrote: > On Wed, Apr 17, 2013 at 6:20 AM, Lele Gaifax wrote: > James Hartley writes: > > Is it possible to map Table instances back to classes defined through > > declarative_base()? > > ...I assume you are asking whether you can map a "view" onto a > Python class using the declarative layer. If so, yes, SQLAlchemy does > not care about how the underlying table is implemented, it will issue > the same SQL in either cases. > > > Thanks for your reply. Any hints on how to tie a class inheriting from Base > to a Table? I have been unsuccessful in bridging the two. 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 -- 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.