The user appcomq encountered an error “key is duplicated” when restoring a backup on their Discourse forum (v1.9.0.beta7 +341). The error message pointed to a duplicated key in the topic_users table. pfaffman suggested a manual method to restore the backup, but appcomq had already lost their forum with 50,000 users after reinstalling the OS.
appcomq investigated the issue by checking the topic_users_id_seq sequence and found a discrepancy. They then identified the problematic row in the topic_users table with id=59700.
To resolve the issue, appcomq manually edited the .sql file, deleting all rows that included the problematic id values (59700 and 11226). This fix successfully restored the forum.
[2017-08-29 15:27:03] ERROR: could not create unique index "index_topic_users_on_topic_id_and_user_id"
[2017-08-29 15:27:03] DETAIL: Key (topic_id, user_id)=(59700, 11226) is duplicated.
[2017-08-29 15:27:03] ERROR: current transaction is aborted, commands ignored until end of transaction block
[2017-08-29 15:27:03] ERROR: current transaction is aborted, commands ignored until end of transaction block
[2017-08-29 15:27:03] ERROR: current transaction is aborted, commands ignored until end of transaction block
[2017-08-29 15:27:03] ERROR: current transaction is aborted, commands ignored until end of transaction block
....
[2017-08-29 15:27:03] EXCEPTION: psql failed
[2017-08-29 15:27:03] /var/www/discourse/lib/backup_restore/restorer.rb:279:in `restore_dump'
/var/www/discourse/lib/backup_restore/restorer.rb:42:in `run'
/var/www/discourse/lib/backup_restore/backup_restore.rb:163:in `block in start!'
/var/www/discourse/lib/backup_restore/backup_restore.rb:160:in `fork'
...