I recently converted my digikam database from MySQL to SQLite. Today I imported some new pictures (by copying them into the appropriate folder and then starting digikam) and was quite surprised that some images already contained tags, even though they came directly from my camera, which doesn't assign any tags. It seems that my database contains lots of tag entries for non-existing images: A "select count(*) from ImageTags Left Join Images on imageid=id where id is Null;" yields "29689" as a result. My guess is that the tags stayed in the database when the images were deleted. I didn't notice this issue before, probably because the auto-increment of the database caused the ids not to be re-used. Apparently this auto-increment status was not transported during migration, and so old tags were applied to new images. (I have verified that the entries in the ImageTags table used for the new image was already present in the old MySQL database, but there was no image with the corresponding id). Shouldn't there be a foreign key constraint in the database, so that the database cannot move to an inconsistent state? At the very least, the database migration tool should clean the database up, so no random tags are assigned to new images. Reproducible: Always
To clean up the leftover tags, use the following command: "delete from ImageTags where imageid not in (select id from Images);"
The problem is worse than described in the original bug report: the ImageTags table is not the only one affected. I also noticed that ImageMetadata and ImageInformation contained lots of stray rows (probably other tables too). It also seems like images get a new id whenever they are moved to a new folder. The symptoms are as follows: When moving a whole folder to a new parent folder, the images in the folder suddenly have wrong metadata (like date taken, thumbnail rotation). This can be corrected by rereading the metadata from disk, but this is of course very annoying.
Swati, This problem is certainly due to the famous bug about lft and rgt ID assigned of tags in Mysql DB. As this bug is fixed now, a fresh Mysql DB converted to SQlite will not have this problem. For older Mysql DB, where ID confusion still exists, i think nothing can be changed/fixed. So, the first stage for this bug is to try to convert a large Tags hierarchy from fresh Mysql DB to Sqlite to see if problem remain. Gilles Caulier
For anyone who has this problem prior to migration to full referential integrity the SQL in the following attachments (as part of https://bugs.kde.org/show_bug.cgi?id=355831 ) will help to identify and remove any bad data in the database. https://bugs.kde.org/attachment.cgi?id=99905 https://bugs.kde.org/attachment.cgi?id=99906
Thomas, Foreign key constraint is now in DB schema (Current version 5.0.0), as a result DB can't contain tags for non existing images.So, I was unable to reproduce this issue. Also, I used the SQL query to verify the same: // MariaDB [digikam]> select count(*) from ImageTags Left Join Images on imageid=id where id is Null; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.01 sec) // The result was 0 even for ImageMetadata and ImageInformation tables. Please try with the recent version again, and report updates if any.
So I have long ago switched to sqlite and have no intention of setting up MySQL just to recheck that bug. If you think the bug should no longer happen, it is OK for me if you just close it.
Ok, Thanks Thomas for your feedback. I close this file now. Don't hesitate to re-open if necessary. Gilles Caulier