<div dir="ltr"><div dir="ltr">Hi Stefan! I answered you on StackOverflow:<br><a href="https://stackoverflow.com/questions/53748730/when-using-pony-orm-or-sqlalchemy-where-to-create-database-object/53765112#53765112">https://stackoverflow.com/questions/53748730/when-using-pony-orm-or-sqlalchemy-where-to-create-database-object/53765112#53765112</a><br><br>Best regards,<br>Alexander</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 13, 2018 at 11:38 AM Stefan Dröge <<a href="mailto:stefan@sdroege.de">stefan@sdroege.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>sorry, forgot to add subject.</div><br><div class="gmail_quote"><div dir="ltr">Am Do., 13. Dez. 2018 um 09:35 Uhr schrieb Stefan Dröge <<a href="mailto:stefan@sdroege.de" target="_blank">stefan@sdroege.de</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><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" target="_blank">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" target="_blank">https://docs.ponyorm.com/api_reference.html#Database.Entity</a></div></div></div></div></div>
_______________________________________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" rel="noreferrer" target="_blank">/ponyorm-list</a><br>
</blockquote></div></div>
_______________________________________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" rel="noreferrer" target="_blank">/ponyorm-list</a><br>
</blockquote></div>