c1 = Customer(id=2, name = "Gopal Krishna", address = "Bank Street Hyarebad", email = "gk@gmail.com")
c1.INVOICE = [Invoice(id=3, invno = 10, amount = 15000), Invoice(id=4, invno = 14, amount = 3850)]
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
session = Session()
session.add(c1)
session.commit()
c2 = [
Customer(
id = 3,
name = "Govind Pant",
address = "Gulmandi Aurangabad",
email = "gpant@gmail.com",
INVOICE = [Invoice(id=5,invno = 3, amount = 10000),
Invoice(id=6,invno = 4, amount = 5000)]
)
]
rows = [
Customer(
id = 4,
name = "Govind Kala",
address = "Gulmandi Aurangabad",
email = "kala@gmail.com",
INVOICE = [Invoice(id=7,invno = 7, amount = 12000), Invoice(id=8,invno = 8, amount = 18500)]),
Customer(
id = 5,
name = "Abdul Rahman",
address = "Rohtak",
email = "abdulr@gmail.com",
INVOICE = [Invoice(id=9,invno = 9, amount = 15000),
Invoice(id=10,invno = 11, amount = 6000)
])
]
session.add_all(c2)
session.commit()
session.add_all(rows)
session.commit()
for c, i in session.query(Customer, Invoice).filter(Customer.id == Invoice.custid).all():
print ("ID: {} Name: {} Invoice No: {} Amount: {}".format(c.id,c.name, i.invno, i.amount))
result = session.query(Customer).join(Invoice).filter(Invoice.amount == 8500)
for row in result:
for inv in row.INVOICE:
print (row.id, row.name, inv.invno, inv.amount)
from sqlalchemy.sql import func
stmt = session.query(
Invoice.custid, func.count('*').label('invoice_count')
).group_by(Invoice.custid).subquery()
for u, count in session.query(Customer, stmt.c.invoice_count).outerjoin(stmt, Customer.id == stmt.c.custid).order_by(Customer.id):
print(u.name, count)
s = session.query(Customer).filter(Invoice.invno.__eq__(12))
s = session.query(Invoice).filter(Invoice.customer.has(name = 'Arjun Pandit'))
s = session.query(Invoice).filter(Invoice.invno.contains([3,4,5]))
s = session.query(Customer).filter(Customer.INVOICE.any(Invoice.invno==11))
from sqlalchemy.orm import subqueryload
c1 = session.query(Customer).options(subqueryload(Customer.INVOICE)).filter_by(name = 'Govind Pant').one()
x = session.query(Customer).get(2)
session.delete(x)
session.query(Invoice).filter(Invoice.invno.in_([10,14])).count()
print (Invoice.custid.foreign_keys)
|