# 고유 제약 조건을 위반하는 중복 키 값 오류로 업그레이드 실패

**URL:** https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767
**Category:** Self-hosting
**Created:** [2월 27, 2020, 10:54오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767 "2020-02-27T10:54:22Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 10:54오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/1 "2020-02-27T10:54:22Z")

</div>

안녕하세요!

오늘 Discourse 인스턴스를 "2.1.0.beta6"에서 최신 버전인 "2.5.0.beta1"으로 업그레이드하려고 시도했습니다.

하지만 웹 업그레이드가 중간에 실패했고, 이후 서버에서 `./launcher rebuild app` 명령을 실행하여 업데이트할 것을 권장하는 메시지가 표시되었습니다.

그렇게 했지만 업그레이드가 다음과 같은 오류와 함께 실패했습니다:

```plaintext
root@cloud:/var/discourse# ./launcher rebuild synfig-forums
Ensuring launcher is up to date
Fetching origin
Launcher is up-to-date
Stopping old container
+ /usr/bin/docker stop -t 10 synfig-forums
synfig-forums
cd /pups && git pull && /pups/bin/pups --stdin
Already up to date.
...
Bundle complete! 124 Gemfile dependencies, 157 gems now installed.
Gems in the groups test and development were not installed.
Bundled gems are installed into `./vendor/bundle`

I, [2020-02-27T10:37:49.176136 #1] INFO -- : > cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate'
2020-02-27 10:38:06.784 UTC [414] discourse@discourse ERROR: duplicate key value violates unique constraint "index_poll_options_on_poll_id_and_digest"
2020-02-27 10:38:06.784 UTC [414] discourse@discourse DETAIL: Key (poll_id, digest)=(30, 399fc6670871474cd7ce0458401fd299) already exists.
2020-02-27 10:38:06.784 UTC [414] discourse@discourse STATEMENT: INSERT INTO poll_options
	  (poll_id, digest, html, anonymous_votes, created_at, updated_at)
	VALUES
	  (30, '399fc6670871474cd7ce0458401fd299', '<ul>
	<li>
	</li>
	</ul>', 0, '2018-07-30 12:09:10 UTC', '2018-07-30 12:09:10 UTC'),(30, '59f25e0e204428418f22b441698f25dd', '**', 0, '2018-07-30 12:09:10 UTC', '2018-07-30 12:09:10 UTC'),(30, '399fc6670871474cd7ce0458401fd299', '<hr>', 0, '2018-07-30 12:09:10 UTC', '2018-07-30 12:09:10 UTC'),(30, '399fc6670871474cd7ce0458401fd299', '<hr>', 0, '2018-07-30 12:09:10 UTC', '2018-07-30 12:09:10 UTC'),(30, '399fc6670871474cd7ce0458401fd299', '<hr>', 0, '2018-07-30 12:09:10 UTC', '2018-07-30 12:09:10 UTC')
	RETURNING digest, id
	
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

ERROR: duplicate key value violates unique constraint "index_poll_options_on_poll_id_and_digest"
DETAIL: Key (poll_id, digest)=(30, 399fc6670871474cd7ce0458401fd299) already exists.
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.6/lib/patches/db/pg.rb:110:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.6/lib/patches/db/pg.rb:110:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.4/lib/mini_sql/postgres/connection.rb:118:in `run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.4/lib/mini_sql/postgres/connection.rb:55:in `query_single'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:131:in `block (2 levels) in up'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:87:in `each'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:87:in `block in up'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:64:in `each'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:64:in `up'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:831:in `exec_migration'
/var/www/discourse/lib/freedom_patches/schema_migration_details.rb:9:in `block in exec_migration'
/var/www/discourse/lib/freedom_patches/schema_migration_details.rb:8:in `exec_migration'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:812:in `block (2 levels) in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:811:in `block in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:810:in `migrate'
/var/www/discourse/lib/migration/safe_migrate.rb:29:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1001:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1311:in `block in execute_migration_in_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1362:in `block in ddl_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1362:in `ddl_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1310:in `execute_migration_in_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1282:in `block in migrate_without_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1281:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1281:in `migrate_without_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1229:in `block in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1382:in `with_advisory_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1229:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1061:in `up'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1036:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/var/www/discourse/lib/migration/safe_migrate.rb:52:in `migrate'
/var/www/discourse/lib/tasks/db.rake:72:in `block in <top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'

Caused by:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_poll_options_on_poll_id_and_digest"
DETAIL: Key (poll_id, digest)=(30, 399fc6670871474cd7ce0458401fd299) already exists.
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.6/lib/patches/db/pg.rb:110:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.6/lib/patches/db/pg.rb:110:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.4/lib/mini_sql/postgres/connection.rb:118:in `run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.4/lib/mini_sql/postgres/connection.rb:55:in `query_single'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:131:in `block (2 levels) in up'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:87:in `each'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:87:in `block in up'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:64:in `each'
/var/www/discourse/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb:64:in `up'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:831:in `exec_migration'
/var/www/discourse/lib/freedom_patches/schema_migration_details.rb:9:in `block in exec_migration'
/var/www/discourse/lib/freedom_patches/schema_migration_details.rb:8:in `exec_migration'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:812:in `block (2 levels) in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:811:in `block in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:810:in `migrate'
/var/www/discourse/lib/migration/safe_migrate.rb:29:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1001:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1311:in `block in execute_migration_in_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1362:in `block in ddl_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1362:in `ddl_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1310:in `execute_migration_in_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1282:in `block in migrate_without_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1281:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1281:in `migrate_without_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1229:in `block in migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1382:in `with_advisory_lock'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1229:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1061:in `up'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/migration.rb:1036:in `migrate'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.1/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/var/www/discourse/lib/migration/safe_migrate.rb:52:in `migrate'
/var/www/discourse/lib/tasks/db.rake:72:in `block in <top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I, [2020-02-27T10:38:06.821527 #1] INFO -- : == 20180820080623 MigratePollsData: migrating =================================
...
I, [2020-02-27T10:38:06.822339 #1] INFO -- : Terminating async processes
I, [2020-02-27T10:38:06.822638 #1] INFO -- : Sending INT to HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main pid: 50
I, [2020-02-27T10:38:06.822850 #1] INFO -- : Sending TERM to exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf pid: 167
2020-02-27 10:38:06.822 UTC [50] LOG: received fast shutdown request
167:signal-handler (1582799886) Received SIGTERM scheduling shutdown...
2020-02-27 10:38:06.825 UTC [50] LOG: aborting any active transactions
167:M 27 Feb 2020 10:38:06.828 # User requested shutdown...
2020-02-27 10:38:06.828 UTC [50] LOG: worker process: logical replication launcher (PID 59) exited with exit code 1
167:M 27 Feb 2020 10:38:06.829 * Saving the final RDB snapshot before exiting.
2020-02-27 10:38:06.839 UTC [54] LOG: shutting down
2020-02-27 10:38:06.941 UTC [50] LOG: database system is shut down
167:M 27 Feb 2020 10:38:07.170 * DB saved on disk
167:M 27 Feb 2020 10:38:07.171 # Redis is now ready to exit, bye bye...

FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 385 exit 1>
Location of failure: /pups/lib/pups/exec_command.rb:112:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
77bc12013304841e8082e888a7a8c86adade040fd97b44fef1c1e85355ba2ae5
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.

```

참고: 제 인스턴스는 `app.yml` 파일이 아니라 `synfig-forums.yml` 파일에 설명되어 있으므로 의도적으로 `./launcher rebuild synfig-forums` 명령을 사용했습니다.

제 이해로는, 다음 줄로 설명되는 문제인 것 같습니다:

```plaintext
ERROR: duplicate key value violates unique constraint "index_poll_options_on_poll_id_and_digest"
DETAIL: Key (poll_id, digest)=(30, 399fc6670871474cd7ce0458401fd299) already exists.

```

이러한 상황에서 무엇을 할 수 있을까요? 도움이 큰 도움이 될 것입니다!

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 1:32오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/2 "2020-02-27T13:32:05Z")

</div>

데이터베이스 내용을 확인하려고 했는데, 다음처럼 진행했습니다:

```plaintext
cd /var/discourse
./launcher start synfig-forums
./launcher enter synfig-forums
su - postgres
psql

```

오류가 발생했습니다:

```plaintext
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2월 27, 2020, 1:38오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/3 "2020-02-27T13:38:10Z")

</div>

인덱스가 손상되었습니다. 손상된 인덱스를 처리하는 방법에 대한 몇 가지 힌트는 [Can't restore due to corrupt indexes (with some clues on how to deal with corrupt indexes)](https://meta.discourse.org/t/cant-restore-due-to-corrupt-indexes-with-some-clues-on-how-to-deal-with-corrupt-indexes/137400) 에서 확인하실 수 있습니다.

별도의 데이터베이스 컨테이너를 사용 중이신가요? 제 추측으로는 사용 중이시며, 해당 컨테이너로 진입해야 할 것 같습니다.

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 1:39오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/4 "2020-02-27T13:39:52Z")

</div>

힌트를 주셔서 감사합니다!

> [@pfaffman](#):
>
> 별도의 데이터베이스 컨테이너가 있습니까? 제 추측으로는 데이터베이스 컨테이너가 따로 있고, 해당 컨테이너로 진입해야 할 것 같습니다.

아니요, 저는 스탠드얼론 컨테이너로 설치했습니다(이렇게 불리는 것 같습니다).

postgresql 서비스를 시작하려고 했습니다(도커 컨테이너에서)가 다음과 같은 오류가 발생했습니다:

```plaintext
root@cloud-synfig-forums:/var/www/discourse# service postgresql start
 * Starting PostgreSQL 10 database server * Error: Config owner (postgres:107) and data owner (syslog:106) do not match, and config owner is not root
                                                                         [fail]

```

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 1:47오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/5 "2020-02-27T13:47:51Z")

</div>

음, postgresql 서비스를 시작하는 데 성공했고, [데이터베이스 백업](https://meta.discourse.org/t/advanced-manual-method-of-manually-creating-and-restoring-discourse-backups/18273)을 수행한 후 데이터베이스에 접속했습니다:

```plaintext
# chown -R postgres /shared/postgres_data
# service postgresql start
 * Starting PostgreSQL 10 database server [OK] 
# psql
psql: FATAL: role "root" does not exist
# su - discourse
$ pg_dump -xOf ~/discourse-backup.sql -d discourse -n public
$ exit
# mv /home/discourse/discourse-backup.sql /shared/discourse-backup-20200227-1.sql
# su - postgres
$ psql discourse
psql (10.4 (Ubuntu 10.4-2.pgdg16.04+1))
Type "help" for help.

discourse=#

```

이제 제 설치를 복구하려면 어떻게 해야 할까요?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2월 27, 2020, 1:48오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/6 "2020-02-27T13:48:09Z")

</div>

좋지 않은 소식인 것 같네요.

재구성을 진행할 때 Postgres 업그레이드에 대한 메시지가 표시되었나요? 재구성을 여러 번 실행해 보셨나요?

최근 백업이 있나요? 백업에서 복원하는 것이 가장 쉬울 수 있지만, 그 또한 쉽지 않을 것입니다.

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 1:57오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/7 "2020-02-27T13:57:47Z")

</div>

> [@pfaffman](#):
>
> 재구축을 수행했을 때 포스트그레SQL 업그레이드에 관한 메시지가 표시되었나요? 여러 번 실행해 보셨나요?

기억이 나지 않습니다. 웹 UI를 통해 업그레이드할 때 메시지가 너무 많았거든요. ☹

그리고 네, 업데이트를 여러 번 실행했습니다. 처음에는 웹 UI에서 (실패했고), 그다음에는 콘솔에서 (여러 번) 실행했습니다.

> [@pfaffman](#):
>
> 최근 백업이 있나요? 그걸로 복원하는 것이 가장 쉬울 수 있지만, 그것도 쉽지 않을 겁니다.

네, 24시간 전에 만든 백업이 있습니다. 복원은 가능하지만, 일부 메시지는 손실될 것입니다(백업 이후에 게시된 메시지들).

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2월 27, 2020, 2:43오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/8 "2020-02-27T14:43:29Z")

</div>

이것은 손상된 인덱스가 아닙니다.

2.1에서 2.5 버전으로 넘어오면서 풀(pools)을 PluginStore에서 정규화된 테이블로 이동했습니다. 이 이동을 수행한 이유 중 하나는 데이터 일관성을 확보하기 위해서였는데, 이는 바로 현재 겪고 계신 문제입니다.

`poll_options` 테이블에 중복된 값이 있습니다. 해당 테이블로 이동하여 중복된 항목을 삭제해 주실 수 있으신가요?

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 2:46오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/9 "2020-02-27T14:46:31Z")

</div>

설명해 주셔서 감사합니다!  
저는 PostgreSQL에 대해 전문가가 아닙니다… 실행에 필요한 명령어를 알려 주실 수 있을까요? (이미 `psql`에 접속해 있는 상태라고 가정하고요.) 🙂

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 4:03오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/10 "2020-02-27T16:03:24Z")

</div>

다음 명령을 실행했습니다:

```plaintext
discourse=# SELECT * FROM poll_options;
 id | poll_id | digest | html | anonymous_votes | created_at | updated_at 
----+---------+--------+------+-----------------+------------+------------
(0 rows)

```

왜 이 테이블이 비어 있을까요?

다른 테이블들은 비어 있지 않습니다:

```plaintext
discourse=# SELECT name FROM badges WHERE id = 13;
    name    
------------
 First Flag
(1 row)

```

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 27, 2020, 4:09오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/11 "2020-02-27T16:09:10Z")

</div>

중복된 항목이 플러그인 스토어에서 비롯된 것으로 추정됩니다. 하지만 해당 항목은 어디에 위치해 있으며, 어떻게 확인하고 수정할 수 있을까요?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2월 27, 2020, 4:47오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/13 "2020-02-27T16:47:08Z")

</div>

그냥 또 다른 테이블(`plugin_store_rows`)입니다.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2월 27, 2020, 4:59오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/14 "2020-02-27T16:59:16Z")

</div>

투표 결과가 실제로는 `post_custom_fields` 테이블에 저장되어 있으며, `name` 열의 값이 'polls’인 것으로 보입니다.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2월 27, 2020, 5:46오후 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/15 "2020-02-27T17:46:04Z")

</div>

이 부분은 [이 커밋](https://github.com/discourse/discourse/commit/4459665deeb84fb43621be90d64378ebc5fbd365)에서 변경되었으며, 해당 마이그레이션은 `poll_options` 테이블을 채웁니다. 따라서 중복 레코드를 삽입하려고 시도하는 것일 수 있습니다.

[https://github.com/discourse/discourse/blob/master/plugins/poll/db/post\_migrate/20180820080623\_migrate\_polls\_data.rb#L132-L138](https://github.com/discourse/discourse/blob/master/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb#L132-L138)

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 28, 2020, 6:29오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/16 "2020-02-28T06:29:14Z")

</div>

그래서 저는 두 가지 옵션을 고려하고 있습니다:

1. 데이터베이스에서 모든 투표 데이터를 삭제하고 업데이트 프로세스를 다시 실행합니다. 이렇게 하면 모든 투표가 손실되지만, 저는 그 점에 대해 괜찮습니다.
2. `migrate_polls_data.rb` 코드를 수정하여 중복 항목의 삽입을 건너뛰도록 합니다. 더 나은 해결책이지만, 어떻게 해야 하는지 모르겠습니다.

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 28, 2020, 6:49오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/17 "2020-02-28T06:49:08Z")

</div>

> [@RGJ](#):
>
> 이 부분은 [이 커밋](https://github.com/discourse/discourse/commit/4459665deeb84fb43621be90d64378ebc5fbd365)에 의해 변경되었으며, 해당 마이그레이션은 `poll_options` 테이블을 채웁니다. 따라서 중복 레코드를 삽입하려는 시도를 하고 있을 수 있습니다.

이 부분이 조용히 실패하도록 코드를 어떻게 수정할 수 있을까요?

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 28, 2020, 6:57오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/18 "2020-02-28T06:57:09Z")

</div>

[https://github.com/discourse/discourse/blob/master/plugins/poll/db/post\_migrate/20180820080623\_migrate\_polls\_data.rb#L132-L138](https://github.com/discourse/discourse/blob/master/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb#L132-L138)

SQL 문장에 `ON CONFLICT DO NOTHING`을 추가하는 것으로 충분할 것 같습니다 - [https://stackoverflow.com/a/31742830](https://stackoverflow.com/a/31742830)  
이렇게 하면 될까요?

---

<div class="post-metadata">

### Author: ![morevnaproject](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/morevnaproject/32/171168_2.png) [@morevnaproject](https://meta.discourse.org/u/morevnaproject)
#### Post date: [2월 29, 2020, 7:19오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/19 "2020-02-29T07:19:13Z")

</div>

음, `pgsql`에서 다음 작업을 수행했습니다:

```plaintext
SELECT *
	FROM post_custom_fields
	WHERE name = 'polls'
	AND value LIKE '%399fc6670871474cd7ce0458401fd299%';

```

1개의 행이 표시되었습니다. 그래서 삭제했습니다:

```plaintext
DELETE FROM post_custom_fields
	WHERE name = 'polls'
	AND value LIKE '%399fc6670871474cd7ce0458401fd299%';

```

참고: 적절한 방법은 행 ID를 기준으로 삭제하는 것이지만, 단일 행만 반환되었기 때문에 그대로 실행했습니다. 🙂

그 후 업그레이드를 다시 시작했고 성공적으로 완료되었습니다. 힌트를 공유해 주신 모든 분들께 감사드립니다! 🙂

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [3월 30, 2020, 7:19오전 UTC](https://meta.discourse.org/t/upgrade-failed-with-duplicate-key-value-violates-unique-constraint-error/142767/20 "2020-03-30T07:19:25Z")

</div>

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