On Wed, 2010-07-07 at 11:13 -0400, thatsanicehatyouh...@mac.com wrote:

> Hi,
> 
> I have a question that I can't find a satisfactory answer to. Apologies in 
> advance if it's more of a Python question, but it's possible that there is a 
> SA solution.
> 
> I have a project that defines a database connection and classes based on 
> database tables. A script that uses these classes would start with:
> 
> import project.DatabaseConnection as db # create_engine() and metadata 
> defined here
> import project.ModelClasses
> 
> In ModelClasses, I define all of the table classes using:
> 
> Base = declarative_base(bind=db.engine)
> 
> class Table1(Base):
>       __tablename__ = 'table1'
>       __table_args__ = {'autoload' : True} # requires metadata to work
> 
> 
> This is fine. I have a second project that also defines a different set of 
> tables that I want to use, so the main script would then:
> 
> import project2.ModelClasses # a different set of tables
> 
> If it's not clear how the "db" parameter (the database connection) was 
> defined in ModelClasses, well, that's my problem. I can't pass a parameter to 
> an import statement of course. The DatabaseConnection class defines the 
> engine and metadata, and now I need to use these objects to generate my base 
> class. How can I pass this object around? Should I be using a different model?


Why not just do this in project2 ?


import project.DatabaseConnection as db

Base = declarative_base(bind=db.engine)

# ... etc.



> The "python way" seems to be to create a "config" class, but 
> project2.ModelClasses won't know anything about it if it's defined in the 
> first project. As to the reason why there are two separate projects, consider 
> the case where one set of tables is one logical group, and the second is a 
> replicated copy from another server. I can't merge all of these projects 
> since they really are independent "units", but sometimes I will link them (as 
> above).



I don't understand why project2 wouldn't "know anything about it" if
defined in (first) project.  All it needs to do is import the connection
info from the project (as in above example).  If the database
configuration really transcends both project and project2 though, then
yes it probably could be wrapped in a config module of some sort in
another project; depending on the scope that may be a bit overkill.  If
you can consider either "project" or "project2" to be slightly more
default than the other then the db config could stay there I'd think.

Lance

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