Rake posts:reorder_posts 出现 `UniqueViolation` 错误

上下文:我正在尝试拆分一个帖子。为此,我认为我需要:

  • 在主题中创建一个新帖子。
  • 编辑两个帖子的内容,按我想要的方式。
  • 将我创建的帖子的所有者更改为与原始帖子匹配。
  • 更改我创建的帖子的时间戳,使其与原始帖子匹配(也许增加几秒钟)。
  • 运行 posts:reorder_posts[topic_id] 将我的回复移到主题中的正确位置。

我尝试的第一件事是调用 posts:reorder_post,“只是看看”,但它失败了:

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)

有趣的是,我在主题中没有看到任何编号为 93 的帖子。我看到 92 和 94。用户 1 是我,但帖子 92 和 94 不是我的。

我之前拆分过主题,所以也许 93 移动到了另一个主题,我不知道。

1 个赞

遇到了同样的问题,同样的跟踪。有解决方案吗?

我认为这不可能。如果一个帖子从主题中拆分出来,帖子编号将会重新计算(因为它不是帖子 ID,而是帖子在主题中的顺序)。它可能是一个小的操作帖子,看起来不像一个“真正的”帖子,但仍然算作一个帖子(低语也算,但如果你正在运行 rake 任务,我假设你是一个可以看到它们的管理员)。

我想删除那个小的操作帖子是否能帮助解决这种情况?这听起来像是你的问题吗 @UndecidedAdmin

2 个赞

我不确定你是什么意思。我不确定“small action post”是什么意思,不过如果它是关于分割帖子的,是的,那可能是问题所在。我遇到了这个错误:

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.

当我访问这个网址时:

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

帖子实际上是 2151,并且显示“Split this topic on Dec 11, 2021”。它上面的帖子是 2143,所以我猜 2150 被移到了另一个帖子,并且某个表对它有了错误的引用。

1 个赞

Unable to move posts between topics - #2 by nat 中,宣布了对此错误的修复。

3 个赞

此主题在最后回复后自动关闭,90 天后。不再允许新回复。