Working with multiple databases

Pony can work with several databases simultaneously. In the example below we use PostgreSQL for storing user information and MySQL for storing information about addresses:

  1. db1 = Database()
  2. class User(db1.Entity):
  3. ...
  4. db1.bind('postgres', ...)
  5. db2 = Database()
  6. class Address(db2.Entity):
  7. ...
  8. db2.bind('mysql', ...)
  9. @db_session
  10. def do_something(user_id, address_id):
  11. u = User[user_id]
  12. a = Address[address_id]
  13. ...

On exiting from the do_something() function Pony will perform commit() or rollback() to both databases. If you need to commit to one database before exiting from the function you can use db1.commit() or db2.commit() methods.