Not a bug All Image Attachments Converted to Links when Deleting Some Attachments

Thanks for the explanation. Makes sense, so then, the hypothesis is the attachment count is erroneously set to 1 when the actual number of attachments is greater than 1, and by deleting 1 image through the core attachment system, XF is simply decrementing the attachment count by 1, resulting in this issue. I'll confirm this hypotheses by the next time this happens. Does XF update the post attach_count when you click Delete on an attachment or when you save the post?
 
FYI, for others with this issue, here's the query to detect where the attach_count is different than the actual # of attachments:

Code:
SELECT xf_post.post_id, S.count, xf_post.attach_count FROM xf_post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment
WHERE content_type = 'post'
GROUP BY content_id) S
ON xf_post.post_id = S.content_id
WHERE xf_post.attach_count <> S.count

You could modify this to perform an update.
 
Based on @Mikes suggestion of what was happening (post attach_count being incorrectly set by add-on, and subsequently decremented to 0), I created the following query to detect where the actual attachment count for a given post_id was different than the post's attach_count:
Code:
SELECT post.post_id, post.attach_count, attachment.count
FROM xf_post as post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment WHERE content_type = 'post' GROUP BY content_id) attachment ON post.post_id = attachment.content_id
WHERE post.attach_count <> attachment.count
Based on analyzing the count mismatches for my recent posts, and knowing what edits I had made to them, I was able to figure out the pattern - the attach_count was equal to the the number of attachments added by Convert Image in the last edit, rather than the total number of attachments. E.g., if you created a post with 3 hotlinked images, Convert Image would create three attachments, and correctly set the post's attach_count to 3. However, if you edited this post and added 1 additional hotlinked image, Convert Image would incorrectly set the attach_count to 1, instead of adding 1 to the existing attach_count (total of 4). If you then subsequently deleted 1 attachment, you would have an attach_count of 0, and images would not be displayed.

I happened to hit this bug a lot due to a specific workflow where I use the RSS feed importer to create posts, which typically have multiple hotlinked images, which Convert Image correctly updated the attach_count for, then I usually edit the post to add a missing "hero" image to the post, at which point Convert Image would set the attach_count to 1, then I often edit the post again to remove 1 pixel invisible tracking images, advertisements, etc.

I have provided the root cause to @AndyB, who has updated Convert Image and Convert Image All XF 1.5 and XF 2.0 add-ons to fix this issue going forward.
Maybe a query can be used to correct this?
You can fix existing attach_count mismatches running the following SQL script in phpMyAdmin, which I have tested on my forum:
Code:
UPDATE xf_post AS post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment
WHERE content_type = 'post'
GROUP BY content_id) attachment ON post.post_id = attachment.content_id
SET post.attach_count = attachment.count
WHERE post.attach_count <> attachment.count
DISCLAIMER: You should backup your site and database before running this script and I am not responsible for any bad things that may happen.
 
Last edited:
I still say the add-on author should provide the fix in his update, the add-on jacked the databases up, so it should fix it.

I hate to think of how many users databases are out there with this issue that do not have a clue of it happening.
 
Keep in mind the updated add-ons only fix the issue for any new posts. Existing edited posts may still have incorrect attach_counts which can still cause this issue even with the fixed add-ons installed. I provided a SQL script above that I used to update all the attach_counts that mismatch.
 
You can fix existing attach_count mismatches running the following SQL script in phpMyAdmin, which I have tested on my forum:
Code:
UPDATE xf_post AS post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment
WHERE content_type = 'post'
GROUP BY content_id) attachment ON post.post_id = attachment.content_id
SET post.attach_count = attachment.count
WHERE post.attach_count <> attachment.count
DISCLAIMER: You should backup your site and database before running this script and I am not responsible for any bad things that may happen.

Worked perfect ;) Thanx!
 
@rfc0001 it’s great you found the root cause at the database level and had a workflow that made it easier to reproduce. I ran into the same symptom with one post when I was editing it to fix formatting awhile ago but worked around it by manually reuploading the attachments at the time. I could not reproduce it at the time.

I only suspected that addon was involved when other admins reported a similar issue with that addon installed but we could not find the root cause other than it occurred after editing a specific post.
 
Back
Top Bottom