# Rake posts:reorder\_posts gives \`UniqueViolation\` error

**URL:** https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098
**Category:** Bug
**Tags:** fixed
**Created:** [February 5, 2023, 10:59am UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098 "2023-02-05T10:59:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Julien\_Palard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julien_palard/32/198784_2.png) [@Julien\_Palard](https://meta.discourse.org/u/Julien_Palard)
#### Post date: [February 5, 2023, 10:59am UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/1 "2023-02-05T10:59:35Z")

</div>

Context: I’m trying to split a post. To do so I think I need to:

- Create a new post in the topic.
- Edit both posts content as I want.
- Change owner of the post I created to match the original one
- Change the timestamp of the post I created to match the original one (maybe adding a few seconds).
- Run `posts:reorder_posts[topic_id]` to move my reply at the right position in the topic.

The first thing I tried was the `posts:reorder_post` invocation, “just to see”, but it failed:

```plaintext
root@discourse1-app:/var/www/discourse# rake "posts:reorder_posts[879]"
rake aborted!
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "post_timings_unique"
DETAIL: Key (topic_id, post_number, user_id)=(879, 93, 1) already exists.
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:110:in `exec'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:110:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/postgres/connection.rb:209:in `run'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/active_record_postgres/connection.rb:38:in `block in run'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/active_record_postgres/connection.rb:34:in `block in with_lock'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/active_record_postgres/connection.rb:34:in `with_lock'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/active_record_postgres/connection.rb:38:in `run'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/mini_sql-1.4.0/lib/mini_sql/postgres/connection.rb:181:in `exec'
/var/www/discourse/lib/tasks/posts.rake:383:in `block (3 levels) in <main>'
/var/www/discourse/lib/tasks/posts.rake:366:in `each'
/var/www/discourse/lib/tasks/posts.rake:366:in `block (2 levels) in <main>'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/transactions.rb:209:in `transaction'
/var/www/discourse/lib/tasks/posts.rake:329:in `block in <main>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Tasks: TOP => posts:reorder_posts
(See full trace by running task with --trace)

```

Intrestingly I don’t see any post numbered 93 in the topic. I see 92 and 94. User 1 is me, but post 92 and 94 are not by me.

I previously split the topic, so maybe 93 moved in another topic, I don’t know.

---

<div class="post-metadata">

### Author: ![UndecidedAdmin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/undecidedadmin/32/212006_2.png) [@UndecidedAdmin](https://meta.discourse.org/u/UndecidedAdmin)
#### Post date: [August 28, 2024, 9:23pm UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/2 "2024-08-28T21:23:59Z")

</div>

running into the same problem, same trace. Is there a resolution?

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [August 28, 2024, 9:44pm UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/3 "2024-08-28T21:44:26Z")

</div>

> [@Julien\_Palard](#):
>
> Intrestingly I don’t see any post numbered 93 in the topic. I see 92 and 94. User 1 is me, but post 92 and 94 are not by me.

I think this isn’t possible. The post\_number would be recalculated if a post was split from the topic (as it’s not a post\_id but the order of the posts in the topic). It could be a small action post that doesn’t look like a ‘real’ post but still counts as one (whispers would also count, but if you’re looking at running rake tasks then I’m assuming you’re an admin who can see them).

I wonder if deleting the small action post would have helped in this situation? Does this sound like it could be the issue for you @UndecidedAdmin?

---

<div class="post-metadata">

### Author: ![UndecidedAdmin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/undecidedadmin/32/212006_2.png) [@UndecidedAdmin](https://meta.discourse.org/u/UndecidedAdmin)
#### Post date: [August 28, 2024, 10:56pm UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/4 "2024-08-28T22:56:36Z")

</div>

> [@JammyDodger](#):
>
> Does this sound like it could be the issue for you @UndecidedAdmin?

I’m not sure I follow what you mean. I’m not sure what a “small action post” is, although if its a post about splitting topics, yes that could be the problem. I’m getting this error:

> PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “post\_timings\_unique” (PG::UniqueViolation)  
> DETAIL: Key (topic\_id, post\_number, user\_id)=(54, 2150, 1) already exists.

When I go to that url:

https://[site]/t/[slug]/54/2150

The post is actually 2151, and its says “Split this topic on Dec 11, 2021”. The post immediately above is 2143, so I am guessing 2150 was moved to a different topic, and some table has the wrong reference to it.

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [December 10, 2024, 9:57am UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/5 "2024-12-10T09:57:50Z")

</div>

In [Unable to move posts between topics - #2 by nat](https://meta.discourse.org/t/unable-to-move-posts-between-topics/341497/2) a fix for this error was announced.

[https://github.com/discourse/discourse/pull/30176](https://github.com/discourse/discourse/pull/30176)

---

<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: [March 10, 2025, 9:58am UTC](https://meta.discourse.org/t/rake-posts-reorder-posts-gives-uniqueviolation-error/254098/6 "2025-03-10T09:58:40Z")

</div>

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