I am trying to compare PonyORM entity instances for identity/equality. Consider the following code, using the estore
example database:
from pony.orm import *
from pony.orm.examples.estore import *
# Test 1
with db_session:
a = Customer[1]
b = Customer[1]
print(a is b, a == b) # True True
# Test 2
with db_session:
a = Customer[1]
with db_session:
b = Customer[1]
print(a is b, a == b) # False False
Entities with the same primary key test as identical and equal if and only if retrieved in the same db_session
.
What is the rationale behind this behaviour? (From the docs: If object with the specified primary key was already loaded into the db_session() cache, Pony returns the object from the cache without sending a query to the database. This explains the result of the identity test. I would still expect to see equality in both test 1 and 2.)
What is the recommended way to test for equality (other than comparing primary keys)?