Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
AssertionError when changing part of a composite key #122
When changing the value of a foreign key attribute, if the attribute is a part of a composite key, then pony throws AssertionError.
This is a semi-minimal setup where the error occurs:
#!/usr/bin/python import pony.orm as po db = po.Database() class A(db.Entity): id = po.PrimaryKey(int, auto=True) adata = po.Required(str) cset = po.Set("C") class B(db.Entity): id = po.PrimaryKey(int, auto=True) bdata = po.Required(str) cset = po.Set("C") class C(db.Entity): id = po.PrimaryKey(int, auto=True) cdata = po.Required(str) akey = po.Required("A") bkey = po.Required("B") po.composite_key(akey, bkey) @po.db_session def init_pony(): a = A(adata="foo") b = B(bdata="bar") C(cdata="baz", akey=a, bkey=b) @po.db_session def break_pony(): newa = A(adata="oof") c = C.get(cdata="baz") c.akey = newa if __name__ == "__main__": db.bind("mysql", user="xxx", passwd="xxx", db="xxx") db.generate_mapping(check_tables=True, create_tables=True) init_pony() break_pony()
This is the traceback:
Judging by variable values in debugger, there appears to be some sort of mismatch:
Is this a bug, or am I doing something wrong?
Thanks for the reporting! This nasty bug remained unnoticed for a long time since release 0.5.2.
In case your are interested, the bug appeared when we ported the code to Python 3. The previous (simplified) code looks like:
attr = ... for attrs, i in attr.composite_keys: vals = map(obj._vals_.get, attrs)
Our Python 3 porting strategy is to make the same source work both in Python 2 and in Python 3. In Python 3 the
So, we changed the above code in the following way:
attr = ... for attrs, i in attr.composite_keys: vals = [ obj._vals_[attr] for attr in attrs ]
Looks clear and more understandable then the previous version. But this code have a nasty bug: the
It is interesting that the Python 3 documentation in such situations recommends to change