[PonyORM-list] Cannot save cyclic chain

Goldberg, Arthur P arthur.p.goldberg at mssm.edu
Mon Nov 3 14:39:10 UTC 2014


Hello Alexander

thank you for your quick and helpful response!
I was able to fix my code with your suggestion.

 Given the inverse references that pony maintains between entities I expect that this Exception will happen frequently. so I recommend that you include a section about it in your documentation.

 Keep up the good work
 regards
 Arthur

On Nov 2, 2014, at 3:41 PM, Alexander Kozlovsky <alexander.kozlovsky at gmail.com<mailto:alexander.kozlovsky at gmail.com>> wrote:

Hi Arthur!

When Pony stores objects to the database, it needs to determine the order of inserts. This order of inserts cannot be arbitrary, because of referential integrity constraints. We cannot insert a row which references to another row, if that another row is not exists in the database yet.

In simple cases Pony can determine valid order of inserts automatically. But in your example there are cyclic references between objects. Pony cannot insert object `ID` with primary key "a", because Pony doesn't know which value to write to the `canonicalEquivalent` column (because the `EquivalentIDset` object is not exists in the database yet). But Pony cannot first save `EquivalentIDset` object also, because referential integrity rules prevent it to store value of "a" in `canonicalID` attribute.

In order to break the cycle you can perform `flush()` command before creating `EquivalentIDset` object:

    a = ID(identifier='a')
    b = ID(identifier='b')
    flush()
    EquivalentIDset(equivalentIDs=[a, b], canonicalID=a)

The `flush()` command forces Pony to store already created `ID` objects. At this moment the saving is possible, because `substituteIDs` and `canonicalEquivalent` attributes of newly created objects don't refer to any object yet, and contain None as a value.

After the `flush()` command is executed, Pony can save `EquivalentIDset` object successfully, because the `ID` object referenced in the `canonicalID` attribute is already stored in the database. After the `EquivalentIDset` object is inserted, Pony will update `a` and `b` objects in order to link them with newly created `EquivalentIDset` object.

Another option is to store all objects right away, but set the value of `canonicalID` attribute separately after the `flush` command:

    a = ID(identifier='a')
    b = ID(identifier='b')
    idset = EquivalentIDset(equivalentIDs=[a, b])
    flush()
    idset.canonicalID = a


Regards,
Alexander


On Sun, Nov 2, 2014 at 7:57 PM, Goldberg, Arthur P <arthur.p.goldberg at mssm.edu<https://urldefense.proofpoint.com/v2/url?u=http-3A__mailto-3Aarthur.p.goldberg-40mssm.edu&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=Im9GDt-vK57s6gDb1QpWydE9Os_4r6XEz5soJaYkN7M&s=kJ2xXdwv-R8Ehl3EmAEjpw4n3qg7Y32txt3ML0WrCkY&e=>> wrote:
Hi Folks

I'm building an alias dbms. Multiple aliases can refer to the same person (genetics subject). I've two main entities: ID, an identifier; and an EquivalentIDset, which points to a set of aliased IDs. EquivalentIDset needs to be a separate entity, so it can have attributes. One of those attributes is canonicalID, the primary ID (if any) among the set.

However, when I set canonicalID and scan through the IDs, I get a 'Cannot save cyclic chain' exception. Here's an (almost) freestanding extract of my code that generates the error:



What does the exception mean?

What's the best way to handle this? My thought is to not define canonicalID as an entity reference, but as a string equal to ID.identifier. But that would lose the dbms consistency benefits. Suggestions?

Thanks
Arthur

PS:
$ pip show pony
---
Name: pony
Version: 0.5.4
Location: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages


_______________________________________________
ponyorm-list mailing list
ponyorm-list at ponyorm.com<mailto:ponyorm-list at ponyorm.com>
/ponyorm-list<https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=Im9GDt-vK57s6gDb1QpWydE9Os_4r6XEz5soJaYkN7M&s=1i-RmtLWX1db57f__7oaYczx8mSRITXwBgO66r3_Q0M&e=>


_______________________________________________
ponyorm-list mailing list
ponyorm-list at ponyorm.com<mailto:ponyorm-list at ponyorm.com>
https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AAIGaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=Im9GDt-vK57s6gDb1QpWydE9Os_4r6XEz5soJaYkN7M&s=1i-RmtLWX1db57f__7oaYczx8mSRITXwBgO66r3_Q0M&e=

---

Arthur Goldberg
Associate Professor of Psychiatry
Seaver Autism Center and Icahn Institute for Genomics & Multiscale Biology
Icahn School of Medicine at Mount Sinai
Seaver Center, Room ABE-33
212-241-4229
Arthur.Goldberg at mssm.edu<mailto:Arthur.Goldberg at mssm.edu>
Follow us on Twitter @IcahnInstitute<https://twitter.com/IcahnInstitute>




-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141103/425fd2d0/attachment.html>


More information about the ponyorm-list mailing list