Skip to content

Instantly share code, notes, and snippets.

@dekoza
Created September 11, 2017 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekoza/038212e18758ce62ab58f0a4bc41edd0 to your computer and use it in GitHub Desktop.
Save dekoza/038212e18758ce62ab58f0a4bc41edd0 to your computer and use it in GitHub Desktop.
PonyORM problem
from models import *
c1 = Customer(email="1@1.com")
c2 = Customer(email="2@1.com")
o1 = Order(customer=c1)
o10 = Order(customer=c1)
o12 = Order(customer=c1)
o11 = Order(customer=c1)
o13 = Order(customer=c1)
o2 = Order(customer=c1)
o20 = Order(customer=c2)
o21 = Order(customer=c2)
o22 = Order(customer=c2)
o23 = Order(customer=c2)
o24 = Order(customer=c2)
p0 = Product()
p1 = Product()
p2 = Product()
p3 = Product()
p4 = Product()
p5 = Product()
p6 = Product()
p7 = Product()
p8 = Product()
p9 = Product()
i0 = OrderItem(product=p0, order=o11)
i1 = OrderItem(product=p1, order=o1)
i2 = OrderItem(product=p1, order=o10)
i3 = OrderItem(product=p6, order=o12)
i4 = OrderItem(product=p0, order=o13)
i5 = OrderItem(product=p2, order=o2)
i6 = OrderItem(product=p3, order=o20)
i7 = OrderItem(product=p1, order=o21)
i8 = OrderItem(product=p2, order=o22)
i9 = OrderItem(product=p2, order=o23)
b0 = Bonus(order_item=i0)
b1 = Bonus(order_item=i1)
b2 = Bonus(order_item=i2)
b3 = Bonus(order_item=i3)
b4 = Bonus(order_item=i4)
b5 = Bonus(order_item=i5, is_used=True)
b6 = Bonus(order_item=i6, is_used=True)
b7 = Bonus(order_item=i7)
b8 = Bonus(order_item=i8)
b9 = Bonus(order_item=i9)
from datetime import datetime
from decimal import Decimal
from pony.orm import * # noqa
db = Database()
class Customer(db.Entity):
id = PrimaryKey(int, auto=True)
email = Required(str, unique=True)
orders = Set('Order')
class Order(db.Entity):
id = PrimaryKey(int, auto=True)
customer = Required(Customer)
order_items = Set('OrderItem')
class OrderItem(db.Entity):
id = PrimaryKey(int, auto=True)
order = Required(Order)
product = Required('Product')
bonuses = Set('Bonus')
class Product(db.Entity):
id = PrimaryKey(int, auto=True)
order_items = Set(OrderItem)
class Bonus(db.Entity):
id = PrimaryKey(int, auto=True)
order_item = Required(OrderItem)
is_used = Required(bool, default=False)
db.bind(provider="postgres", user="postgres", password="postgres",
host="localhost", database="pony", port="3306") # the port param is not documented!
db.generate_mapping(create_tables=True)
c = Customer.select().random(1)[0] # example
query = left_join(
(
p,
count(i.order.customer == c),
count((b.is_used == True) for b in i.bonuses),
count(i)
) for p in Product
for i in p.order_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment