On Nov 17, 2008, at 7:17 AM, Power Button wrote:

>
> What I have tried is putting all the setup instructions into a class
> and instantiating this in myApp.py. This doesn't make it global to all
> the modules though. Do I need to pass this object around to all
> classes as arguments to __init__() in order to reference this class?
> (this seems a bit bulky to me)


Neither your classes, mappers, or Table objects need any knowledge of  
the Session or engine in order to be defined.  Table objects only need  
a MetaData() object, which you probably want to define once in a  
single module, which is imported by all of your other modules.   If  
using declarative, then your declarative base class takes the place of  
the MetaData object.

The Session and engine should also be instantiated once globally.   
When you start using the Session against particular classes, that's  
when they have some association with the database.


> Here is my Bootstrap class
>
> class Bootstrap(object):
>    def __init__(self):
>        self.mDb = MyDb() #my DB abstraction class
>        self.mEngine = self.mDb.getEngine()
>        self.mSession = self.mDb.getSession()
>        self.mMeta = self.mDb.getMeta(self.mEngine)
>
>        if self.mTable1 == None:
>            self. mTable1 = self.mDb.getTable("table1", self.mMeta)
>            self.mEdoMapper = mapper(Table1, self. mTable1)
>
>        if self.mTable2 == None:
>            self. mTable2 = self.mDb.getTable("table2", self.mMeta)
>            self.mEdoMapper = mapper(Table2, self. mTable2)

this is overly complex.  You dont need any reference to mapper  
objects, and the Table objects don't need to be so formally assigned  
either.  A simple setup looks like:

base.py
----------
# global MetaData object
metadata = MetaData()

model1.py
--------------
import base
class MyClass(object):
     .....

t1 = Table('sometable', base.metadata, ...)

mapper(MyClass, t1)

# more classes and tables

model2.py
--------------
import base

class SomeOtherClass(object):
      .....
t1 = Table('....', ....)
mapper(SomeOtherClass, t2, ...)

bootstrap.py
----------------
# configure the database connection/session

import base
engine = create_engine('my engine://')
Session = scoped_session(sessionmaker(bind=engine))

def create_tables():
     """create all tables which don't yet exist in the DB."""

     base.metadata.create_all(engine)

application.py
-------------------
from bootstrap import Session
frmo model1 import MyClass

print Session.query(MyClass).all()


I would also recommend the usage of declarative, 
http://www.sqlalchemy.org/docs/05/plugins.html#plugins_declarative 
  , which simply means that the separate class/Table/mapper() objects  
are defined at once, and it also allows dependencies between mapped  
classes to be defined using strings which can reduce or remove  
circular dependency issues.



--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to