On Apr 11, 2013, at 6:49 AM, Treeve Jelbert <tre...@scarlet.be> wrote:

> I am using reflection on an existing database
> 
> ##############
> engine = create_engine(login.connection)
> 
> metadata = MetaData(bind=engine)
> Base = declarative_base(metadata=metadata)
> Base.metadata.bind = engine
> #Base.metadata.reflect()
> Base.metadata.reflect(views=True)
> 
> 
> try:
>   Base.metadata.create_all()
> except Exception, e:
>   print 'unable to access database, check the username/password\n',e
>   sys.exit()
> ########################
> 
> 
> 
> If I  activate __tablename__ and  __table_args__ for Invoice and Expense, 
> sqlalchemy generates SQL which references non-existant tables.
> 
> If I manually add views for  Invoice and Expense everything works.

working backwards, this means you're starting with a DB that does not have 
"invoice" or "expense", and I guess you want them to be views.   There's not 
much built-in for CREATE VIEW (which also requires a SELECT construct, since 
that's what a view comes from), we do have a recipe you can use that's here: 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Views

however it might be easier just to do literal DDL for those views, ie. you 
write out the "CREATE VIEW" as you are, but to add it to your process you can 
use the DDL() construct:

from sqlalchemy import DDL, event

event.listen(metadata, "after_create", DDL("CREATE VIEW invoice ..."))
event.listen(metadata, "after_create", DDL("CREATE VIEW expense ..."))

that doesn't yet solve the problem of Invoice/Expense having Table metadata 
that's creating, a quick way to omit them is just to give them their own 
metadata, using a base class:

class ViewMetaData(Base):
    __abstract__ = True
    metadata = MetaData()  # replaces Base.metadata for subclasses

class Invoice(ViewMetaData):
    __tablename__ = 'invoice'

   # ...


haven't run these examples here, hopefully I got them right.




> 
> 
> I am using latest rel_0_8 branch
> 
> -- 
> 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.
> 
> 

-- 
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