On Tue, May 20, 2008 at 12:42 PM, Luis Bruno <[EMAIL PROTECTED]> wrote:
> Mike Orr escreveu:
>>
>> If you create a new app in Pylons' development version, it will
>> ask if you want SQLAlchemy, and if so it will preconfigure the model
>> according to that article.
>>
>
> Care to change it to a I-know-who-my-session-is model? As you can see below,
> the session itself doesn't need to know what bind= it will use. Doing it the
> way in the pylonsdocs hinders multiple databases in the same session (but in
> different MetaData()s.
>
> ---8<---
> def __init__(bind):
>   metadata.bind = bind
>
> def __setup__(bind):
>   metadata.bind = bind
>   metadata.create_all()
>   # XXX: hookup sa-migrate
>
> metadata = sa.MetaData()
>
> Session = scoped_session(sessionmaker(transactional=True, autoflush=True))
>
> table = Table(...)
>
> class Stuff(object):
>   pass
>
> Session.mapper(Stuff, table)

The default model was chosen after extensive discussions with Michael
Bayer, SQLAlchemy's creator, when SQLAlchemy 0.4 was in beta.  He
regretted putting bound metadata in tutorials because people were
blindly using it without really understanding it.  (He also dropped a
DynamicMetaData which was even more misused.)  Mike recommended bound
sessions instead, so I have merely followed his advice.

This was actually just one of several issues where SQLAlchemy 0.3 had
gotten too magical and messy ("several ways to do it"), and the Pylons
0.9.5 default model was very bad.  So we all agreed on certain best
practices for SQLAlchemy 0.4, which guided this article and the
default model.

I should mention that the separate 'meta' module and the init_model()
function were not part of the original recommendation, since they were
invented later.

In the SQLAlchemy manual Mike has kept silent about some of his
opinions.  Several features in SQLAlchemy remain only because of user
demand, and not because Mike thinks they're the best way.  For
instance, Session.mapper().  So he moved metadata.bind from the
tutorial to a later section but doesn't argue for or against it.  I
personally think he should make his recommendations explicit in the
manual, but instead he's chosen to neutrally support everything that's
not outright bad.

There's one practical problem with bound metadata.  If you use
transactional sessions and non-ORM commands simultaneously, the
non-ORM commands are outside the transaction.  That was part of the
motivation for recommending bound sessions.  Mike added
Session.execute(...) to allow non-ORM commands to be executed within
the transaction, so the article recommends this.

If you have multiple databases, you can bind different tables to
different engines using the binds={...} argument to sessionmaker().
You can also change the bindings at any time via
"Session.configure(binds=)"  or "session = Session();  session.binds =
{...}".  I hinted that in the article but did not go into details
because advanced users will find it, along with metadata.bind, in the
SQLAlchemy manual.  If you think multiple databases should be
supported more in the article, please add a comment to the article and
it will be considered for a future version.

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to