On Jul 24, 2007, at 12:09 PM, alex.schenkman wrote:

> class Session(object):
>     def __init__(self, name):
>         self.db = create_engine( 'sqlite:///%s' % name )
>         self.db.echo = False
>         self.metadata = BoundMetaData(self.db)
>         self.session = create_session()
>         self.db.docs = Table('docs', self.metadata, autoload=True)
>         self.db.mapper = mapper(Document, self.myDB.docs)
>

mappers are at the same level at which your mapped class is defined.   
So if you define your Document class at the module level, so must  
your Mapper be defined.  also, if you defined classes and mappers  
within a function for each session, that wouldnt scale anyway since  
the mappers get stored in a global registry and youd run out of  
memory after many distinct users visited the site.

so if your mapper is at the module level, so are your Tables and  
MetaData.   Sessions are not; so bind your individual sessions to the  
engines directly.  (Engines are usually module level too, but in this  
case you are opening many individual sqlite files so theyre local to  
your Session object)


metadata = MetaData()
class Document(object):
        pass

# cant autoload=True here unless you have a specific SQLite file that  
is safe to use.  doesnt your
# system need to create the tables inside the sqlite databases anyway ?
docs = Table('docs', metadata,
        Column(...)
)

mapper(Document, docs)

class Session(object):
        def __init__(self, name):
                self.db = create_engine('sqlite:///%s' % name)
                self.session = create_session(bind = self.db)

thats it.  you dont need to reference "mapper" anywhere, just  
"self.session" and maybe "docs".



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