# ERROR RecordNotUnique on index\_discourse\_solved\_solved\_topics\_on\_answer\_post\_id

**URL:** https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875
**Category:** Support
**Tags:** solved
**Created:** [March 26, 2025, 6:39am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875 "2025-03-26T06:39:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [March 26, 2025, 6:39am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/1 "2025-03-26T06:39:29Z")

</div>

I’m getting this error when trying to update via

`./launcher rebuild app`

```plaintext
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: could not create unique index "index_discourse_solved_solved_topics_on_answer_post_id" (ActiveRecord::RecordNotUnique)
DETAIL: Key (answer_post_id)=(201795) is duplicated.

```

Any ideas on how to resolve this issue?

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [March 26, 2025, 8:00am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/3 "2025-03-26T08:00:42Z")

</div>

Delete one of the [`SolvedTopic`](https://github.com/discourse/discourse-solved/blob/main/app/models/discourse_solved/solved_topic.rb) records via rails console:

```ruby
DiscourseSolved::SolvedTopic.where(answer_post_id:201795).last.destroy

```

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [March 26, 2025, 9:16am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/4 "2025-03-26T09:16:20Z")

</div>

Thank you. I’ve tried to access those, but I’m getting

`NameError: uninitialized constant SolvedTopic`

Any other ideas?

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [March 26, 2025, 9:32am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/5 "2025-03-26T09:32:34Z")

</div>

I’ve now connected to the database directly and deleted the record with the duplicate `answer_post_id` from `discourse_solved_solved_topics`.

All good now. Thanks again for your quick reply!

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [March 26, 2025, 12:59pm UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/6 "2025-03-26T12:59:30Z")

</div>

Hm. discourse-solved was [updated recently](https://github.com/discourse/discourse-solved/pull/342).

There is a chance, that this error was thrown during database migration.

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [March 26, 2025, 2:19pm UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/7 "2025-03-26T14:19:23Z")

</div>

Yes, this happened during database migration.

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [March 26, 2025, 5:04pm UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/8 "2025-03-26T17:04:38Z")

</div>

Seems to be fixed with this patch:

> <https://github.com/discourse/discourse-solved/commit/1805184cde87d2621f1252612f142f4bbbf03a79>
>
> We are seeing some errors when migrating and adding indexes on \`answer\_post\_id\`.…
> 
> \`\`\`
> \#\<StandardError:"An error has occurred, all later migrations canceled:\\n\\nPG::UniqueViolation: ERROR: could not create unique index \\"index\_discourse\_solved\_solved\_topics\_on\_answer\_post\_id\\"\\nDETAIL: Key (answer\_post\_id)=(13006) is duplicated.\\n"\>
> \`\`\`
> 
> This PR modifies the earlier migration, and also adds one before the addition of indexes to remove duplicates.

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [March 26, 2025, 5:07pm UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/9 "2025-03-26T17:07:31Z")

</div>

Excellent! Thanks for the heads up — I’ve marked your post as solution.

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [April 9, 2025, 7:25am UTC](https://meta.discourse.org/t/error-recordnotunique-on-index-discourse-solved-solved-topics-on-answer-post-id/358875/10 "2025-04-09T07:25:08Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
