# Error on attempting to remap links

**URL:** https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443
**Category:** Bug
**Created:** [September 10, 2023, 3:39am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443 "2023-09-10T03:39:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [September 10, 2023, 3:39am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/1 "2023-09-10T03:39:08Z")

</div>

I’m getting this error when attempting to remap links in my recently moved instance as per [Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-your-discourse/16098)

> root@ehealthforum-nz-syd01-web-only:/var/www/discourse# discourse remap [draft.ehealthforum.nz](http://draft.ehealthforum.nz) [ehealthforum.nz](http://ehealthforum.nz)  
> Rewriting all occurrences of [draft.ehealthforum.nz](http://draft.ehealthforum.nz) to [ehealthforum.nz](http://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](https://meta.discourse.org/t/upgrade-fails-on-duplicate-key-error-index-post-hotlinked-media-on-post-id-and-url-md5/226002), but has occurred on remapping instead.

It seems to occur on posts with Oneboxes. Of which there are many!

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [September 11, 2023, 2:33am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/3 "2023-09-11T02:33:20Z")

</div>

It is going to take us a bit, but we should get this sorted in the next month or so.

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [September 11, 2023, 4:21am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/4 "2023-09-11T04:21:09Z")

</div>

Would this work / be wise in the meantime? It is causing some pain.

> [@Rebake fails when changing the subdomain name only](https://meta.discourse.org/t/rebake-fails-when-changing-the-subdomain-name-only/128104/5):
>
> 1. This time links are partial so I used a replace query:
> 
> ```plaintext
> DB.exec "update topic_links set url=REPLACE(url, 'discourse.coretabs.net', 'discourse-old.coretabs.net')"
> 
> ```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [September 11, 2023, 4:31am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/5 "2023-09-11T04:31:33Z")

</div>

Nope, you need to fix a different table

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [September 12, 2023, 6:38pm UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/7 "2023-09-12T18:38:06Z")

</div>

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:

> [@Set up Let’s Encrypt with multiple domains / redirects](https://meta.discourse.org/t/set-up-let-s-encrypt-with-multiple-domains-redirects/56685):
>
> NOTE: @pfaffman says: This page needs to be cleaned up. There is now a new ENV setting that will let you add more hostnames. In your app.yml under your DISCOURSE\_HOSTNAME line (it can go many places but that one makes sense), add DISCOURSE\_HOSTNAME\_ALIASES: domain.com,other.domain.com and early reports suggest that you’ll get valid certs for those domains and that accessing https://domain.com will properly redirect you to your DISCOURSE\_HOSTNAME without a certificate error. If you do that an…

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.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [September 27, 2023, 7:38am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/8 "2023-09-27T07:38:17Z")

</div>

Update, I am sorry, but we are totally swamped at the moment. I am putting #pr-welcome on this for now.

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [November 18, 2023, 10:42pm UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/10 "2023-11-18T22:42:32Z")

</div>

This error seems to have gone away now. Has it been fixed?

However, I’ve got [a new one](https://meta.discourse.org/t/remap-error-when-a-post-contains-a-quote-from-another-topic/285950) for remapping! Will report separately.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [November 20, 2023, 6:05am UTC](https://meta.discourse.org/t/error-on-attempting-to-remap-links/278443/11 "2023-11-20T06:05:12Z")

</div>


