getId KeyError: None - When Saving

I have a strange issue after migrating my Plone 3 database over to Plone 5. Now when I edit one of the documents inside a folder I get the following error when saving...

Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module Products.PDBDebugMode.runcall, line 70, in pdb_runcall
  Module ZPublisher.Publish, line 48, in call_object
  Module plone.z3cform.layout, line 66, in __call__
  Module plone.z3cform.layout, line 50, in update
  Module plone.dexterity.browser.edit, line 58, in update
  Module plone.z3cform.fieldsets.extensible, line 59, in update
  Module plone.z3cform.patch, line 30, in GroupForm_update
  Module z3c.form.group, line 145, in update
  Module plone.app.z3cform.csrf, line 21, in execute
  Module z3c.form.action, line 98, in execute
  Module z3c.form.button, line 315, in __call__
  Module z3c.form.button, line 170, in __call__
  Module plone.dexterity.browser.edit, line 30, in handleApply
  Module z3c.form.group, line 126, in applyChanges
  Module zope.event, line 31, in notify
  Module zope.component.event, line 24, in dispatch
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module zope.component.event, line 32, in objectEventNotify
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module plone.app.linkintegrity.handlers, line 111, in modifiedContent
  Module plone.app.linkintegrity.handlers, line 141, in updateReferences
  Module z3c.relationfield.event, line 140, in _setRelation
  Module zc.relation.catalog, line 539, in index_doc
  Module zc.relation.catalog, line 469, in _indexNew
  Module zc.relation.catalog, line 570, in _getValuesAndTokens
  Module z3c.relationfield.relation, line 27, in from_id
  Module five.intid.intid, line 41, in getId
  Module zope.intid, line 84, in getId
KeyError: None

The interesting thing is when I duplicate the object and paste it then there are no issues editing the subsequent item. The only difference I can see between the original and the copied document is a lock icon next to the creators name. I had a look into this (https://docs.plone.org/4/en/develop/plone/functionality/locks.html) and it seems that maybe something relating to the content lock is the culprit but I'm not sure...

This is on Plone 5.0.7...

Any help on this would be appreciated, thanks!

I re-ran the site migration and unlocked all locked items in the catalog. However this did not seem to have any effect...
Any ideas peeps..?

I would try to fix the catalogs. Rebuild the catalog.

OK, it turns out this issue was coming from images that were included in the RichText fields of the item. These images had broken image paths (not handled in the migration) and required manually checking the fields and updating the image 'resolveuid' paths with the corresponding updated image path format used in Plone 5. In this case something like '/@@images/image/thumb' or the sorts...
This was the script I ran: fix image scales from archetypes scales to dx scales, old scale names to new scale names · GitHub
Just a note for future persons who might have this issue!

References:

Just want to put further on this for reference.
I did more testing on this and it seems the following happened:

  • I ran the custom script to fix all the resolveuid paths for objects provided by the RichText interface
  • Images are now displayed correctly in both my custom views and in the TinyMCE editor
  • Saving any changes to the 'text' field (RichText), specifically when adding an image or saving with the existing image causes the KeyError traceback
  • I created a clean, new item with which I was able to save an image in the RichText field without issues (this is under the same path)
  • Adding a debug statement I debugged and investigated the differences between the content saved on the broken (migrated) item and the new one, fortunately they're both the same ruling out any content issues
  • I tried copying the structure code form TinyMCE on the working item and attempted to copy it into the broken one, to which it fails...

Ok, so at this point I was very confused. In my migration for my custom Content-Type items I use plone.api to create new instances of the old Archetype objects and merely copy the field values over. So the objects themselves are clean (so to speak) but still broken. At this point I checked all the reference catalogs i.e. uid_catalog and reference_catalog, but these are deprecated, nevertheless I remove all the entries and try the above steps again.

It's still broken...

Alright, so now I'm totally stumped. I know there's no problem with the content itself, the fields are saving correctly and the object itself is in essence new. The ONLY thing I can see that has changed is that the path to the object (which was an Archetype object) is now a Dexterity object. So the actual path itself is the same. So here comes the strange part. Merely renaming the object allowed me to save the content, no KeyErrors, images there, all good. So now I tried renaming the object back to its old path/id. It works.

So to sum up the testing, something was causing some method, somewhere to go haywire and return the wrong value, either because of some content lock or a broken reference to the object path. Renaming this back and forth seems to have rereferenced or reindexed something..?

Very strange issue with a very simple resolution. Though would be nice if someone could explain this..?

the traceback shows this is related with the link integrity feature and is probably caused by a missing integer ID on the referenced object; you probably forgot to copy that from the old content type instance.

check zope.intid for more information.