Thanks again for you enlightening answer.
I had no understanding for these different scopes.

What I haven't managed is to call a db function from a session/query
object.

I had the following line in my code, but it doesn't work anymore.
select([func.max(table.c.DocID)]).execute().scalar()

How would I call the max function having a query or session object, as
in the example below?

Thanks again!


from sqlalchemy import *

metadata = MetaData()
docs = Table('docs', metadata)
docs.append_column(Column('DocID', Integer, primary_key=True))
docs.append_column(Column('Path', String(120)))
docs.append_column(Column('Complete', Boolean))

class Doc(object):
    def __init__(self, id, path, state):
        self.DocID = id
        self.Path = path
        self.Complete = state

    def __str__(self):
        return '(%s) %s %s' %(self.DocID, self.Path, self.Complete)

if __name__ == "__main__":
    mapper(Doc, docs)

    db = create_engine( 'sqlite:///mydb' )
    db.echo = False

    s = create_session(bind_to = db)
    q = s.query(Doc)



On Jul 24, 6:36 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> 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