Search code examples
ormponyorm

An attempt to mix objects belonging to different transactions


pony.orm.core.TransactionError: An attempt to mix objects belonging to different transactions

I know that this error is descriptive, I just can't figure out WHY its occurring. I have searched google, the docs, other SO posts, and found nothing.

Any ideas?


Solution

  • This error arises if somebody attempts to interlink two objects which belongs to identity maps of two different db sessions:

    from pony.orm.examples.university1 import *
    
    populate_database()
    
    with db_session:
        g = Group[101]
    
    with db_session:
        s = Student[4]
        s.group = g  # or g.students.add(s)
    

    Each db session should work with objects retrieved within this db session only. If you need to store some information between db sessions, you can store object's id value instead of object itself. Also you can pickle objects in one db_session and then unpickle them in another db_session.