PonyORM is a powerful ORM package that is written in pure Python. It is fast and easy to use and performs operations with minimum effort. It provides automatic query optimization and a GUI database schema editor. It also supports automatic transaction management, automatic caching, and composite keys. PonyORM uses Python generator expressions, which are translated in SQL. We can install it using pip:
$ pip install pony
Let's see an example of ORM using pony:
# Import pony module
from pony.orm import *
# Create database
db = Database()
# Define entities
class Emp(db.Entity):
eid = PrimaryKey(int,auto=True)
salary = Required(int)
# Check entity definition
show(Emp)
# Bind entities to MySQL database
db.bind('mysql', host='localhost', user='root', passwd='12345', db='employee')
# Generate required mappings for entities
db.generate_mapping(create_tables=True)
# turn on the debug mode
sql_debug(True)
# Select the records from Emp entities...