# Remap failure when changing discourse domain

**URL:** https://meta.discourse.org/t/remap-failure-when-changing-discourse-domain/306470
**Category:** Self-hosting
**Created:** [May 2, 2024, 1:10pm UTC](https://meta.discourse.org/t/remap-failure-when-changing-discourse-domain/306470 "2024-05-02T13:10:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![fpoulain](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fpoulain/32/251282_2.png) [@fpoulain](https://meta.discourse.org/u/fpoulain)
#### Post date: [May 2, 2024, 1:10pm UTC](https://meta.discourse.org/t/remap-failure-when-changing-discourse-domain/306470/1 "2024-05-02T13:10:13Z")

</div>

Hi,  
I tried to change a discourse domain name, following [Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-your-discourse/16098). Things looked right until I launched the command

```plaintext
discourse remap talk.foo.com talk.bar.com

```

I got the following:

```plaintext
root@plop:/var/www/discourse# discourse remap plop.domain.fr forum.plop.fr
Rewriting all occurrences of plop.domain.fr to forum.plop.fr
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)
YES
backup_metadata=1
email_logs=991
javascript_caches=1
post_revisions=126
post_search_data=20
posts=782
stylesheet_cache=446
theme_settings=1
Error: ERROR: duplicate key value violates unique constraint "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(274, 455, http://forum.plop.fr) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

It seemed it looked like to the error reported [here](https://meta.discourse.org/t/rebake-fails-when-changing-the-subdomain-name-only/128104), but was slightly different. I guess the bug is related to the fact that the new domain has been discussed on the forum.

To overcome it, I was unable to launch something like `db:exec` to force db commands via `rake`. So I did it directly via postgres:

1. connect the dababase:

```plaintext
# cd /var/discourse/
# ./launcher enter app
# su - postgres
$ psql
postgres=# \c discourse

```

1. try to directly replace url in the table

```plaintext
discourse=# update topic_links set url=REPLACE(url, 'plop.domain.fr', 'forum.plop.fr');
ERROR: duplicate key value violates unique constraint "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(274, 455, http://forum.plop.fr) already exists.

```

1. on error : force a replacement that don’t break unicity, e.g. `forum.plop.fr/`:

```plaintext
discourse=# update topic_links set url=REPLACE(url, 'plop.domain.fr', 'forum.plop.fr/') where topic_id=274;

```

1. repeat 3. until 2. finishes without error. Then quit pg.

```plaintext
discourse=# update topic_links set url=REPLACE(url, 'plop.domain.fr', 'forum.plop.fr');
UPDATE 1349
discourse=# 
\q

```

1. relaunch the remap and the rebake:

```plaintext
$ exit
# discourse remap plop.domain.fr forum.plop.fr

```

1. Manually check and correct the problematic topics (which content may be inconsistent), by visiting `forum.plop.fr/t/topic/274` and others.

---

<div class="post-metadata">

### Author: ![fpoulain](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fpoulain/32/251282_2.png) [@fpoulain](https://meta.discourse.org/u/fpoulain)
#### Post date: [May 2, 2024, 1:15pm UTC](https://meta.discourse.org/t/remap-failure-when-changing-discourse-domain/306470/2 "2024-05-02T13:15:24Z")

</div>

> [@fpoulain](#):
>
> `discourse remap talk.foo.com talk.bar.com`

If I correctly understand the problem _a posteriori_, the [doc](https://meta.discourse.org/t/change-the-domain-name-or-rename-your-discourse/16098) could suggest to remove all old occurrences of `talk.bar.com` prior to launch this command. It could simply done via

```plaintext
discourse remap talk.bar.com talk.foo-or-bar-that-s-the-question.com

```

(I didn’t tried.)
