On Feb 8, 2011, at 1:19 PM, Martijn Moeling wrote:

> Hi,
> 
> I am having a small issue with multiple python modules and declarative...
> 
> I might miss something but....
> 
> Consider:
> 
> a.py:
> 
> 8<-------------------------------
> Base = declarative_base()
> 
> class A(Base):
>       ...
> 8<-------------------------------
> 
> b.py
> Base = declarative_base()
> 
> class B(Base):
>       ...
> 8<-------------------------------
> 
> c.py
> Base = declarative_base()
> 
> class C(Base):
>       ...
> 8<-------------------------------
> 
> d.py
> 
> Base = declarative_base()
> 
> from A import * # imports base.... 
> from B import * # imports base....
> from C import * # imports base....
> 
> Class D1(Base)
> ...
> 
> Class D2(A)
> ...
> 
> 
> in d.py I want to create:
> 
> def create_tables(engine):
>       metadata= Base.metadata
>       metadata.create_all(engine)
> 
> 
> Is there any way to properly "add" the metadata from the imported modules in 
> d.py  to the Base.metadata during the import......?
> Think of modules a,b,c and d are together in a package and d is imported with 
> similar packages into something bigger....

Usually the convention is that all modules in an application share the same 
declarative base object (i.e. Base).   If you wanted multiple Base objects but 
have them share a common MetaData, you can declare the MetaData up front, then 
create each Base using declarative_base(metadata=my_metadata).     Otherwise if 
you're really looking to merge together multiple MetaData objects, that's not 
really in the API right now in a clean way, you'd perhaps call .tometadata() on 
each Table object but that's really not what I would do here - it copies the 
whole Table object and isn't really for a use case like this.   If you can at 
least have a common MetaData object, or better a common Base object, that would 
be the best way to go.


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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.

Reply via email to