Pony ORM Release 0.4.9

In this release we added new functionality which was requested by Pony users. The main features include the possibility to delete tables in the database and recreate tables with all necessary foreign key constraints and indexes. Let’s see how it works in detail.

Drop tables

There are 4 methods for removing tables in the database, which you can use now:

Where db is an instance of the Database class.

This method drops all tables, which are related to the current mapping. When this method is called without parameters, Pony will remove tables only if none of them contain any data. In case at least one of them is not empty the method will raise the TableIsNotEmpty exception without dropping any table. In order to drop tables with the data you should pass the parameter with_all_data=True: drop_all_tables(with_all_data=True).

If you specify with_all_data=True, then Pony will remove all tables which are mapped to declared entities even if they have data. This parameter has the same meaning for all methods below.

db.drop_table(table_name, if_exists=False, with_all_data=False)
Where db is an instance of the Database class.

This method drops a table. If such table doesn’t exist the method raises the exception TableDoesNotExist. Note, that table_name is case sensitive. If the parameter if_exists is set to True, then it will not raise the TableDoesNotExist exception if there is no such table in the database.

Where MyEntity is a declared entity class.
This method removes a table which is mapped to the entity.

Where MyEntity is a declared entity class and my_collection is a Set attribute:

    class MyEntity(db.Entity):
        my_collection = Set(…)

This method drops a table, which is associated with the Set attribute. The primary role of this method is to drop an intermediate table, which is used for establishing many-to-many relationship between two Set attributes. But if you call this method for many-to-one relationship, it will try to remove the table used for the entity at the other side of the many-to-one relationship.

Create tables

Pony now has a separate method for creating tables and corresponding foreign keys and indexes:

Where db is an instance of the Database class.
This method checks the existing mapping and creates tables for entities if they don’t exist. Also, Pony will check if foreign keys and indexes exist and create them if they are missing.

In previous releases the method generate_mapping() always checked if the tables in the database match with the entities. Now you can switch this check off. This can be useful if you want just generate mapping and create tables later.
This gives you the following options for mapping generation:

– db.generate_mapping(create_tables=True) – create tables, foreign key references and indexes if they don’t exist
– db.generate_mapping() – assuming that tables are already exist, check if the existing tables match with the enitites
– db.generate_mapping(check_tables=False) – neither create nor check tables, useful if you plan to call db.create_tables() later.

Other new features

Starting with the release 0.4.9, Pony automatically enables foreign key support in SQLite. By default SQLite doesn’t check foreign key constraints, and in previous Pony releases foreign key constraints in SQLite were not enforced.

Where db is an instance of the Database class.
Close the database connection for the current thread if it was opened.

Comparison methods for the Entity class were added. Now entity instances can be sorted by its primary key values.

Entity.exists(…) method was added. You can check the entity attributes for equality:


or use a lambda condition:

Order.exists(lambda o: o.total_price > 500 and count(o.items) <=3)

Reported bugs were fixed, new tests were added.

Thanks to all Pony users for sending us their feedback and telling us how they are using Pony. We appreciate it!
As always we are looking forward to your comments and suggestions at our mailing list ponyorm-list.ponyorm.com