> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:sqlalch...@googlegroups.com] On Behalf Of Chris Withers
> Sent: 01 July 2010 19:17
> To: sqlalchemy@googlegroups.com
> Subject: [sqlalchemy] models in different packages, often 
> declaratively defined
> 
> Hi All,
> 
> Suppose I have packageA that defines:
> 
> from sqlalchemy.ext.declarative import declarative_base
> Base = declarative_base()
> class User(Base)
>    __tablename__ = 'user'
>    ...
> 
> Now, I have a packageB that defines:
> 
> from sqlalchemy.ext.declarative import declarative_base
> Base = declarative_base()
> class Something(Base)
>    ...
> 
> I want Something to have a foreign key that points at User.
> How do I do that?
> 
> The tables for packageA and packageB should exist in the same 
> database 
> (the extraction of User into packageA is just so that all our 
> projects 
> that need "users" get the same schema for the 'user' and 
> related tables 
> and functionality for users).
> 
> I guess things could be engineered such that one MetaData instance is 
> shared between all the bases (how would that be done though? 
> I'd need to 
> get the MetaData instance into each of the packages before 
> declarative_base is called...)
> 
> Moreover, how do I get all the Base's to share a _decl_class_registry?
> (I'm still hazy on why the information in 
> _decl_class_registry can't go 
> into MetaData, rather than having two registries...)
> 
> Any ideas gratefully received...
> 
> Chris
> 

If packageB depends on packageA, I would have packageB import the
metadata or declarative Base class from packageA. Otherwise, I would
create a new package, (called something like 'common'), which creates
the metadata and declarative Base class. packageA and packageB would
import those items from the common package.

I imagine that by having a single declarative Base class, the
_decl_class_registry problem will disappear. I also assume that the
reason that isn't stored in the MetaData is that MetaData is an object
provided by the underlying sql library, whereas 'declarative' is an
extension to the ORM, and the MetaData class shouldn't know anything
about it.

Simon

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

Reply via email to