I did finally manage to get my hierarchical tags back in order, though it took many hours of experimenting to figure out the procedure. If anyone else is faced with rearranging tags, @ashurbanipal describes the procedure much better than I could have done.
The only thing I will add to Steve’s “how to” is that when rearranging tags it’s a good idea to run digiKam from a terminal. What tipped me off as to “why” just moving an entire branch back to where it belongs was failing every time, was the following terminal output:
digikam.general: Delete Shortcut assigned to tag 56
digikam.dbengine: Failure executing query:
"UPDATE Tags SET pid=? WHERE id=?;"
Error messages: "Unable to fetch row" "UNIQUE constraint failed: TagsTree.id, TagsTree.pid" 19 1
Bound values: (QVariant(int, 81), QVariant(int, 56))
Whenever a message abut “UNIQUE contraint failed” appears, even though the digiKam user interface “looks” like the tag has been moved, it really hasn’t.
That’s a good idea, something I haven’t done yet. My usual way to “clean up the database” is to simply delete the existing database and then recreate, after using exiftool to spot-check to make sure that the proper tags are actually in the image files. I still don’t trust digiKam to write metadata, especially to raw files. But I do trust exiftool, which is why I have digiKam set up to only write to sidecar files. But maybe the “clean up the database” tools are working properly in the current version of digiKam?
I actually set digiKam to not write anything at all while I was rearranging the tags. And then I wrote them all at once when I was done rearranging. And I was very careful to make backups before writing anything at all, which was a good thing to do as it took several trials (and many hours) to figure out how to get digiKam to write things out properly.
It used to matter quite a lot when writing to sidecar files. I don’t know about now, stiill testing.
This issue with not being able to rearrange the tag tree without risking severe corruption of the hierarchy has been a problem with digiKam for a long time.
The recent version of digiKam has an option that seems to help. Under “Settings/Configure digiKam/Metadata/Advanced” you can configure digiKam to only write selected metadata items. I never did add useful ratings/labels/captions to my images (maybe some day). So the only metadata I was concerned about was the image tags. So I set digiKam to only read and write one item, which is “Tags/Xmp.digiKam.TagsList”.
I also took the precaution of using exiftool to remove all non-camera-written fields, so that literally the only user-added metadata in the image files was the digiKam TagsList, which was only written to the Xmp field. Before resorting to this rather drastic measure, after allowing digiKam to write to the image file (contrary to my usual practice but other efforts had failed), upon closing and reopening digiKam the tag tree was littered with a huge number of single-level tags that would have taken forever and a day to get rid of, except that fortunately I was working on a copy of the images.
Steve’s “how to” seems to indicate that even after getting the tags back in order “the first time”, any effort to do future rearrangement still isn’t a simple case of drag and drop. Sigh.