A user, nathank, encountered an error when attempting to remap links in their Discourse instance after a domain change. The error occurred due to a duplicate key value in the index_post_hotlinked_media_on_post_id_and_url_md5 constraint, likely caused by Oneboxes in posts.
nathank tried to find a solution, and sam mentioned it would take some time to fix. In the meantime, nathank considered a temporary workaround, but sam advised against it.
nathank eventually found a workaround by setting up a CNAME record and validating it with Let’s Encrypt, which redirected broken links to the correct place.
Later, sam marked the issue as #pr-welcome due to being swamped with tasks. The error eventually disappeared, but nathank encountered another remapping issue, which was reported separately.
root@ehealthforum-nz-syd01-web-only:/var/www/discourse# discourse remap draft.ehealthforum.nzehealthforum.nz
Rewriting all occurrences of draft.ehealthforum.nz to ehealthforum.nz
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)
YES
Error: ERROR: duplicate key value violates unique constraint “index_post_hotlinked_media_on_post_id_and_url_md5”
DETAIL: Key (post_id, md5(url::text))=(80591,66a0d020b208dc9f8452a5eb9617f6bc) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.
While a bit obscure, this is likely to trip up others. It is similar to this, but has occurred on remapping instead.
It seems to occur on posts with Oneboxes. Of which there are many!
I’ve got a workaround which has fixed the immediate problem for me. I’ve set up a CNAME record from the ‘old’ URL (which happens to be a subdomain of my new root), and validated it with Let’s Encrypt as per this guide:
Now the broken links at least get the user to the correct place!
I’m a little unclear why I had the problem. An automated remap seemed to occur the first time I restored my backup on my staging instance (before making it live). Subsequent restores didn’t trigger a remap despite the instance URL changing - this confused me, and has let to this current problem.