# Import keeping old post ids in Discourse

**URL:** https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852
**Category:** Migration
**Tags:** kunena
**Created:** [October 18, 2018, 10:53am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852 "2018-10-18T10:53:27Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 18, 2018, 10:53am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/1 "2018-10-18T10:53:27Z")

</div>

I am halfway through a migration from Kunena, I have (almost) successfully ran a test migration, but as I think about the permalinks problem, and also internal links problem, I wonder…

Would it be hard to have Discourse create the new posts keeping the old post ids that Kunena originally assigned? There are obvious advantages to this approach.

I guess this would involve

1. Some tweak to the import script. Can anyone estimate how “deep” a change this would be? Is post id assigned very deep into Discourse code, or somewhere in the base import script?

2. Some way to keep Discourse from assigning already used post ids to new posts in the future. Is there somewhere we can configure Discourse to start assigning post ids from 100.000 onwards, for example?

3. Can post ids be non numeric? If so, I was thinking that “k12345” (for stuff coming from Kunena) could be a nice half-way solution. I keep a way to reference old post ids, but they wouldn’t conflict with new Discourse-assigned ids.

Thanks in advance for any pointers!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 18, 2018, 11:46am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/2 "2018-10-18T11:46:38Z")

</div>

The import script saves the import id in a post custom field. You can use it to create permalinks. Several importers do that. You can look at others for examples.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 18, 2018, 11:49am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/3 "2018-10-18T11:49:32Z")

</div>

Thanks Jay, I know that, and I use that to go from old ids to new ids.

But it would be more practical to simply use the same number and avoid that de-referencing. I am doing parts of my import with SQL and it complicates queries.

Also, the permalinks table can get quite huge. A regexp redirect is better, and easy if the id is constant.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 18, 2018, 11:53am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/4 "2018-10-18T11:53:36Z")

</div>

The permalinks table is only as big as the post table. You know about permalink normalizations?

But to answer your question, yes, having discourse use different topic id would be very hard.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 18, 2018, 12:09pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/5 "2018-10-18T12:09:07Z")

</div>

It seems Discourse relies on PostgreSQL to assign the id, which is numeric:

 ![image](https://global.discourse-cdn.com/meta/original/3X/c/6/c6fdd783a23cc0b2aa33458a02d1ed7b5196fb52.png)

And it seems PostgreSQL allows manipulation of that “sequence object”:

> **[9.17. Sequence Manipulation Functions](https://www.postgresql.org/docs/8.3/functions-sequence.html)**
>
> 9.17. Sequence Manipulation Functions # This section describes functions for operating on sequence objects, also called sequence generators or just sequences. …

So I guess my number 2 is answered “yes” and my number 3 is answered “no”. My question number 1 is probably what you mean by “very hard”, right? ☹

EDIT: let me just add that `posts` uses the same mechanism as `topics`

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 18, 2018, 12:16pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/6 "2018-10-18T12:16:21Z")

</div>

> [@pfaffman](#):
>
> You know about permalink normalizations?

I had heard about these regexp operations but I was thinking they _required_ post ids to match so a simple find-and-replace could take me from the old post id to the new one.

Or is the mechanism more intelligent and it does a lookup on the post custom field to translate ids?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 18, 2018, 1:38pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/7 "2018-10-18T13:38:38Z")

</div>

Yes. You still need to have the post (or topic or category) ID in a permalink.

Your solution might work with a plugin that you would have to continually maintain, but it’ll be really hard, and the recommended way works and has been used dozens of times, including several with millions of posts, and that’s just me.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 18, 2018, 1:44pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/8 "2018-10-18T13:44:17Z")

</div>

The approach (without keeping a plug in all the time) could be:

1. Disable automatic serial numbers for topics
2. Import everything with the old ids
3. Re-enable serials, starting at a number larger than the MAX currently used

But ok, I get the point - it’s easier with the permalinks feature. I have found many posts with specific questions about this, mostly people trying to get their regexps right, but I haven’t found any page documenting the actual mechanism in general… is there such a page?

As always, thanks a lot for your help.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [October 22, 2018, 6:06pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/9 "2018-10-22T18:06:14Z")

</div>

Can anybody point to any Documentation on how to use the permalinks feature? Even if it’s minimal docs. I don’t mind helping to write a more extended documentation once I understand how this works. Thanks.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 22, 2018, 8:18pm UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/10 "2018-10-22T20:18:31Z")

</div>

The best thing to do is to `grep Permalink script/import-scripts/*rb` and look at how people have used it.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 21, 2021, 3:57am UTC](https://meta.discourse.org/t/import-keeping-old-post-ids-in-discourse/99852/11 "2021-07-21T03:57:03Z")

</div>


