By database we refer to a separate instance (could be on another
server) of equvalent database i.e. production and development
environments.

Using sqlalchemy.ext.declarative, what would be a correct pattern to
enable one model to connect to two equivalent databases?

Consider:

--- model.py ---
Base = declarative_base()
metadata = Base.metadata

class MyTable(Base):
    __tablename__ = 'my_table'
    id = Column(Integer, primary_key=True, nullable=False)
    description = Column(CHAR(length=100), nullable=False)

_Session = sessionmaker()

def make_session(uri):
    metadata.bind = engine
    session = Session(bind=engine)
    return session

--- program.py --
import model

"""copy some data between databases"""
srcdb = model.make_session(srcdb_uri)
destdb = model.make_session(srcdb_uri)

items = srcdb.query(model.MyTable).all()

for i in items:
    destdb.merge(i)

destdb.commit()

---

We have two distinct sessions bound to to different databases but
because metadata is a module level variable it gets rebound to the
database of last call.

Also I am somewhat unclear on the difference between metadata binding
to engine and session binding to engine, especially when they end up
different. What is relevance of MetaData binding?

--

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.


Reply via email to