On May 11, 2010, at 11:32 AM, Martin Bacovsky wrote:
> Hi all, > > I need to add support for db views in our project. I'm limited to SA 0.5 at > the moment and need the > views to be included during create_all/drop_all calls. > > I followed the recomended recipe > http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Views and modified > it work with SA 0.5. My View function looks like this: > > > def View(name, metadata, selectable): > > t = Table(name, metadata) > > for c in selectable.c: > c._make_proxy(t) > > create_ddl_1 = "DROP TABLE %s" % (name) > create_ddl_2 = "CREATE VIEW %s AS %s" % ( > name, selectable) > drop_ddl_1 = "DROP VIEW %s" % (name) > drop_ddl_2 = "CREATE TABLE %s(id integer)" % (name) > > DDL(create_ddl_1).execute_at('after-create', t) > DDL(create_ddl_2).execute_at('after-create', t) > DDL(drop_ddl_1).execute_at('before-drop', t) > DDL(drop_ddl_2).execute_at('before-drop', t) > > return t > > The problem is that SA doesn't know that the view has to be created after the > referenced tables > (I'm not sure if this issue is resolved in the recipe/SA 0.6 at all). > > I monkey-patched sqlalchemy.sql.util.sort_tables to include my dependency > tuples, but I'd like to > have nicer solution. Is there one? If not what are the flaws in my plan, I > have to be prepared for? you should set the create views for the "after-create" event of the Metadata itself. An API that supports the addition of arbitrary dependency tuples to be used in sort_tables might be nice someday as well but I don't think you need it here (such an API would mean that there would have to be some way to add "creates" to the metadata that aren't table objects, too). -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.