Hi,
Just wanted to say thanks to those who helped me with this. Simon's solution
was exactly what I was looking for (though I have to admit I don't exactly
understand *how* it works!). But that's no longer an SQLAlchemy question...
Cheers,
Demitri
On Jul 8, 2010, at 5:49 AM, King Simon-NFHD78 wrote:
> In general, you don't need a database connection just to define your
> tables and mappers. The 'bind' parameter to DeclarativeBase is optional,
> and only necessary if you are using autoloading. So one solution to your
> problem would be not to use autoloading, and bind to a database at the
> Session level rather than the Mapper level. That would be the usual way
> to use the same set of classes against multiple databases.
>
> If you really need to use autoloading, you could move all your class
> definitions into a function that accepts a database engine as a
> parameter. For example:
>
> #################
> # ModelClasses.py
>
> class Namespace(object):
> def __init__(self, **kwargs):
> self.__dict__.update(kwargs)
>
>
> def initdb(connection_string):
> engine = create_engine(connection_string)
> Base = declarative_base(bind=engine)
>
> class Table1(Base):
> __tablename__ = 'table1'
> __table_args__ = {'autoload': True}
>
>
> return Namespace(Base=Base,
> Table1=Table1)
>
> # or, you could be lazy:
> # return Namespace(**locals())
>
>
>
> ####################
> # MainScript1.py
> import ModelClasses
>
> db = ModelClasses.initdb(my_connection_string)
>
> # access db.Table1, db.Base etc.
>
>
>
> Hope that helps,
>
> Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.