{"id":8436,"library":"pony","title":"Pony ORM","description":"Pony ORM is a Python Object-Relational Mapper that allows developers to work with databases using Python objects and queries written in plain Python. It translates Python queries into optimized SQL, supporting SQLite, PostgreSQL, MySQL, and Oracle. The current version is 0.7.19. Releases occur periodically, often driven by new Python version support or significant bugfixes, indicating an active but not rapid development cadence with several months to a year between minor releases.","status":"active","version":"0.7.19","language":"en","source_language":"en","source_url":"https://github.com/ponyorm/pony","tags":["ORM","database","SQL","data-modeling"],"install":[{"cmd":"pip install pony","lang":"bash","label":"Install Pony ORM"}],"dependencies":[],"imports":[{"symbol":"Database","correct":"from pony.orm import Database"},{"symbol":"Required","correct":"from pony.orm import Required"},{"symbol":"Optional","correct":"from pony.orm import Optional"},{"symbol":"Set","correct":"from pony.orm import Set"},{"symbol":"PrimaryKey","correct":"from pony.orm import PrimaryKey"},{"symbol":"db_session","correct":"from pony.orm import db_session"},{"symbol":"select","correct":"from pony.orm import select"}],"quickstart":{"code":"from pony.orm import *\n\ndb = Database()\n\nclass Person(db.Entity):\n    id = PrimaryKey(int, auto=True)\n    name = Required(str)\n    age = Optional(int)\n    cars = Set('Car')\n\nclass Car(db.Entity):\n    make = Required(str)\n    model = Required(str)\n    owner = Required(Person)\n\ndb.bind(provider='sqlite', filename=':memory:', create_db=True)\ndb.generate_mapping(create_tables=True)\n\n@db_session\ndef create_data():\n    p1 = Person(name='John Doe', age=30)\n    c1 = Car(make='Toyota', model='Camry', owner=p1)\n    p2 = Person(name='Jane Smith', age=25)\n    print(\"Data created.\")\n\n@db_session\ndef query_data():\n    for p in select(p for p in Person if p.age < 35):\n        print(f\"Name: {p.name}, Age: {p.age}\")\n        for car in p.cars:\n            print(f\"  Car: {car.make} {car.model}\")\n\ncreate_data()\nquery_data()","lang":"python","description":"This quickstart demonstrates defining a database connection, creating entity models, generating the database schema, and performing basic CRUD operations within a `db_session` context. It uses an in-memory SQLite database for simplicity."},"warnings":[{"fix":"Upgrade Python to 3.8+ or pin Pony ORM to a compatible version.","message":"Pony ORM 0.7.17 dropped support for Python versions older than 3.8. Users on Python 3.7 or earlier must either upgrade their Python environment or use an older Pony ORM version (e.g., 0.7.16 or earlier).","severity":"breaking","affected_versions":"<0.7.17"},{"fix":"Upgrade Pony ORM to version 0.7.15 or newer for Python 3.10 and above.","message":"Pony ORM versions prior to 0.7.15 were incompatible with Python 3.10+ due to reliance on the deprecated `parser` module. Attempting to use older versions on Python 3.10+ will result in import errors.","severity":"breaking","affected_versions":"<0.7.15"},{"fix":"Always wrap database interactions within a `db_session`.","message":"All database operations (entity creation, updates, queries) must occur within an active `db_session`. Forgetting to use the `@db_session` decorator or `with db_session():` context manager will lead to `TransactionError` or `ObjectIsFreedException`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Familiarize yourself with Pony's Pythonic query syntax as described in the official documentation.","message":"Pony ORM's unique query language uses generator expressions (e.g., `select(p for p in Person if p.age < 35)`). New users often try to use SQL strings or SQLAlchemy-style expressions, which will not work as expected.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Wrap your database operations within a `with db_session():` block or decorate functions with `@db_session`.","cause":"Trying to access or modify Pony ORM entities outside of an active `db_session` context.","error":"TransactionError: An attempt to work with detached object"},{"fix":"Upgrade Pony ORM to version 0.7.16 or newer. This bug was resolved in that release.","cause":"An internal bug in Pony ORM versions prior to 0.7.16, specifically affecting `db.bind()` calls with certain argument combinations.","error":"TypeError: DBAPIProvider.__init__() got multiple values for argument 'database'"},{"fix":"While Pony ORM includes some reconnection logic, ensure each logical unit of work is encapsulated within a `db_session`. For very long-running applications, consider explicit connection management or shorter `wait_timeout` on the database.","cause":"Network instability, database server restart, or connection timeouts for long-running processes that hold open connections.","error":"OperationalError: (2013, 'Lost connection to MySQL server during query')"},{"fix":"Upgrade Pony ORM to version 0.7.15 or newer, which transitioned to using Python's standard `ast` module.","cause":"Using Pony ORM older than 0.7.15 with Python 3.10+. Python 3.10 removed the `parser` module which older Pony versions relied upon.","error":"AttributeError: module 'pony.orm.parser' has no attribute 'suite'"}]}