# Import scripts fail to relate topics and posts

**URL:** https://meta.discourse.org/t/import-scripts-fail-to-relate-topics-and-posts/60377
**Category:** Development
**Created:** [April 3, 2017, 12:56pm UTC](https://meta.discourse.org/t/import-scripts-fail-to-relate-topics-and-posts/60377 "2017-04-03T12:56:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Matias\_Hernandez](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Matias\_Hernandez](https://meta.discourse.org/u/Matias_Hernandez)
#### Post date: [April 3, 2017, 12:56pm UTC](https://meta.discourse.org/t/import-scripts-fail-to-relate-topics-and-posts/60377/1 "2017-04-03T12:56:34Z")

</div>

I’m writing an import script for a custom forum.  
The importing of categories and users works good, but the script (using the `create_posts` method) is not able to build the relation between the topic and the replies.

The relevant code

```
//Topics
create_posts(threads, total: total) do |t|
  source_user_id = @usernamesMap[t["username"]]
  {
    id: t["id"],
    user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
    raw: t["Thread_Description"],
    created_at: t["created_at"],
    posts_count: t["count"],
    category: category_id_from_imported_category_id(t["Topic_ID"]),
    title: t["title"]
  }
end

// Replies
create_posts(posts, total: total) do |t|
  next unless topic = topic_lookup_from_imported_post_id(t["Thread_ID"])
  source_user_id = @usernamesMap[t["username"]]
  opts = {
     id: t['id'],
     user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
     raw: t["Post_Description"],
     created_at: t["created_at"],
     title: t["title"],
     topic_id: topic_lookup_from_imported_post_id(t["Thread_ID"]), //This is not working correctly
     reply_to_post_number: topic_lookup_from_imported_post_id(t["Thread_ID"])
   }
end

```

Any ideas?

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [April 3, 2017, 6:21pm UTC](https://meta.discourse.org/t/import-scripts-fail-to-relate-topics-and-posts/60377/2 "2017-04-03T18:21:58Z")

</div>

The `topic_lookup_from_imported_post_id()` method returns a hash.

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/base/lookup_container.rb#L92-L96>

So, I’d rewrite your function to something like this:

```ruby
create_posts(posts, total: total) do |t|
  parent = topic_lookup_from_imported_post_id(t["Thread_ID"])
  next unless parent
  source_user_id = @usernamesMap[t["username"]]
  opts = {
     id: t['id'],
     user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
     raw: t["Post_Description"],
     created_at: t["created_at"],
     title: t["title"],
     topic_id: parent[:topic_id],
     reply_to_post_number: parent[:post_number]
   }
end

```

---

<div class="post-metadata">

### Author: ![paultaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paultaylor/32/166024_2.png) [@paultaylor](https://meta.discourse.org/u/paultaylor)
#### Post date: [January 10, 2020, 12:55pm UTC](https://meta.discourse.org/t/import-scripts-fail-to-relate-topics-and-posts/60377/3 "2020-01-10T12:55:31Z")

</div>

Trying to work out how to create topic so owned by a particular user rather than adminuser  
running the script.

> [@With Api how do you create topics and posts so owned by particular user](https://meta.discourse.org/t/with-api-how-do-you-create-topics-and-posts-so-owned-by-partiocular-user/138235):
>
> Importing my data from csv files using the api. I’m importing my users successfully. I’m importing categories, topics and posts successfully, except at the moment all my topics and posts are shown as being created by me the discourse admin for the site. Not clear how I create them so owned by the actual user Does the correct user have to be specified as the Api-Username for each post, and if so does that mean the user has to be an admin user (which they wouldnt be). Or do I create the topics…

From your post looks like you can just pass user\_id to api although not documented, but it doesn’t seem to work for me. Is that the case and if so is user\_id the **id** column of the users table or the **username** column?
