The order of resolution is described here:

http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#sqlalchemy.orm.session.Session.get_bind

Binds on the session take priority over binds on metadata (passing an
engine to declarative_base binds the metadata for that base class).

As far as best practice goes, I would say that it depends on the kind
of program you are writing. Tables (and mapped classes and so on) tend
to be created at module scope. Binding an engine at that point means
that you need to know the database credentials at the time your module
is imported. This is often fine for quick scripts, but can become
annoying when structuring a larger application. For a larger
application, it is probably better to define the structure of your
data model separately from an actual connection to the database.

Binding at the session level gives you more flexibility, as you can
decide on a per-session basis which engine you want to use (for
example if you had different engines for read and write access).

Hope that helps,

Simon

On Wed, Mar 12, 2014 at 7:25 AM, Bao Niu <[email protected]> wrote:
> Ok, let me try rephrasing my question.
> Is binding an engine/connection simultaneously to a Session and a Table
> considered bad practice? I've looked up the documentation but not
> sufficiently confident about this. There seems no definite/official answer
> to this. Could some experienced users help give a definite clue here?
> Thanks.
>
>
> On Sunday, March 9, 2014 12:08:42 AM UTC-8, Bao Niu wrote:
>>
>> From reading the documentation I learned that you can either bind an
>> engine to a session:
>> >>>engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
>> >>>Session = sessionmaker(bind=engine)
>>
>>  or to a declarative_base:
>> >>>engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
>> >>>Sqlalchemy_base = declarative_base(engine)
>>
>> Is there a best practice like always binding to a session?
>> I currently bind the engine to both:
>> >>>engine = create_engine('sqlite:///{}'.format(dbPath), echo=False)
>> >>>Session = sessionmaker(bind=engine)
>> >>>Sqlalchemy_base = declarative_base(engine)
>> From running my codes I didn't encounter any problem. But I wonder how
>> SqlAlchemy resolves the conflict internally if there are two bindings? Is
>> there some potential problems down the road if I kept using this manner?
>> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to