Hi Michael,

Thanks for the reply.

On Thu, 19 May 2011 10:59:18 -0400, Michael Bayer <mike...@zzzcomputing.com> 
wrote:

> I'm sure I mentioned earlier, one of the reasons you're finding this
> difficult is because of this type of pattern, where you're calling
> mapper() and Table at a different scope than where you make your
> classes i.e. within a callable versus module level.  At the very
> least a function like make_tables() would ensure that its internal
> implementation is called only once, such as:

> _table_dict = None
> def make_tables():
>     global _table_dict
>     if _table_dict is not None:
>         return _table_dict
>    _table_dict = {.... }

> otherwise, you're creating new MetaData objects each time,
> rebuilding a who= le new set of Tables in it each time, i.e. its a
> total mess.  Engine, Session registry, MetaData, your set of classes
> and their mappers, these should all be global objects, created once
> per application. If you want to use functions to initiate their
> existence that is fine but you should be maintaining singleton
> behavior in any case.

You make good points. This certainly makes sense if I am only dealing
with one set of database tables at a time. However, I'm working with
multiple datasets, sometimes within a single Python script. So I guess
some extra "wipe the slate clean" stuff when switching between
different datasets is needed here.

A couple of comments/questions.

I see you are using _table_dict as an indicator. I'm not sure what

_table_dict = {.... }

is intended to indicate. Do you just mean initialize _table_dict with
some non-None value before proceeding with the function?

So if I am switching data sets, I guess I would need to recreate the
tables, presumably by creating a new MetaData.  Since the classes
don't change, they don't need to be removed. So, would it sufice to
have

*********************************************************************

do stuff with dataset 1

clear_mappers()
_table_dict = None   # so the make_tables will be rerun and MetaData recreated

do stuff with dataset 2

*********************************************************************

Hmm. maybe clear_mappers should be inside make_tables?

Am I missing anything?
                                                               Faheem

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to