<div dir="ltr"><div dir="ltr"><div dir="ltr">I already asked the question on stackoverflow, however I just found this mailing list, so I thought I'd cross post my question here:</div><div dir="ltr"><br></div><div dir="ltr">(Original question from Stackoverflow: <a href="https://stackoverflow.com/questions/53748730/when-using-pony-orm-or-sqlalchemy-where-to-create-database-object">https://stackoverflow.com/questions/53748730/when-using-pony-orm-or-sqlalchemy-where-to-create-database-object</a> )</div><div dir="ltr"><div><br></div><div><div>I began playing a bit with Object Relational Mappers (*Pony ORM* specifically).</div><div><br></div><div>In *Pony*, all entity definitions inherit from the [`db.Entity`][1] class. However in order to do that of course the `db` object needs to be created first somewhere. (`db.Entity` is somewhat similar to the declarative base in *sqlalchemy*, so I believe my question below is also similar valid for *sqlalchemy*)</div><div><br></div><div>All the examples I have seen in the Pony ORM docs present the examples inline, where the Database object `db` is simply declared in the interpreter prompt before the entities are declared.</div><div><br></div><div>This leaves for me the question: **Where should I create my db object in a "real" project?.**</div><div><br></div><div>Especially consider the case where I want to keep my entity definitions separate from where I actually use these entities (say I only want to build a nice ORM wrapper package to access a database, which then should be used in multiple different other projects). Then I probably want the users to provide the own `db` object that is configured to their needs in order to access the DB.</div><div><br></div><div>## Some example code:</div><div><br></div><div>Say I have a Database that stores *persons* and *addresses* and my package `my_orm` should provide an ORM for the database, which then will be used in `app.py`:</div><div><br></div><div><br></div><div>*my_orm/__init__.py*</div><div><br></div><div>    from my_orm.person import Person</div><div>    from my_orm.address import Address</div><div><br></div><div>*my_orm/person.py:*</div><div><br></div><div>    from pony.orm import Required</div><div>    </div><div>    class Person(db.Entity): # Where should `db` be defined?</div><div>        name = Required(str)</div><div>        age = Required(int)</div><div><br></div><div>*my_orm/address.py:*</div><div><br></div><div>    from pony.orm import Required</div><div>    </div><div>    class Address(db.Entity): # Where should `db` be defined?. Must be the same `db` object as in person.py</div><div>        street_name = Required(str)</div><div>        zip_code = Required(int)</div><div><br></div><div>*app.py*</div><div><br></div><div>    from pony.orm import Database</div><div>    db = Database()</div><div>    import my_orm</div><div><br></div><div>Besides already looking ugly because it mixes the imports with the creation of the db, this will also throw the error `NameError: name 'db' is not defined`. So what should I do instead?</div><div><br></div><div>  [1]: <a href="https://docs.ponyorm.com/api_reference.html#Database.Entity">https://docs.ponyorm.com/api_reference.html#Database.Entity</a></div></div></div></div></div>