Pony examples

Instead of creating models manually, you can check the examples from the Pony distribution package:

  1. >>> from pony.orm.examples.estore import *

Here you can see the database diagram for this example: https://editor.ponyorm.com/user/pony/eStore.

During the first import, there will be created the SQLite database with all the necessary tables. In order to fill it in with the data, you need to call the following function:

  1. >>> populate_database()

This function will create objects and place them in the database.

After the objects have been created, you can try some queries. For example, here is how you can display the country where we have most of the customers:

  1. >>> select((customer.country, count(customer))
  2. ... for customer in Customer).order_by(-2).first()
  3. SELECT "customer"."country", COUNT(DISTINCT "customer"."id")
  4. FROM "Customer" "customer"
  5. GROUP BY "customer"."country"
  6. ORDER BY 2 DESC
  7. LIMIT 1

In this example, we are grouping objects by the country, sorting them by the second column (the number of customers) in the reverse order, and then extracting the first row.

You can find more query examples in the test_queries() function in the pony.orm.examples.estore module.