# Update failed - PG::UniqueViolation: ERROR

**URL:** https://meta.discourse.org/t/update-failed-pg-error/103684
**Category:** Self-hosting
**Created:** [December 7, 2018, 8:51am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684 "2018-12-07T08:51:54Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AlexMl](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@AlexMl](https://meta.discourse.org/u/AlexMl)
#### Post date: [December 7, 2018, 8:51am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/1 "2018-12-07T08:51:54Z")

</div>

Hi guys,

I tried to update Discourse forum to the latest version 2.2.0.beta5 on stage instance, but it failed.

The error is:

```
I, INFO -- : > cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate'
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_polls_on_post_id_and_name"
DETAIL: Key (post_id, name)=(662593, poll) already exists.

I logged into postgresql database and checked that there are no values in polls table, so it is strange why do we receive an error - "Key (post_id, name)=(662593, poll) already exists.":
=> SELECT * FROM polls;
id | post_id | name | close_at | type | status | results | visibility | min | max | step | anonymous_voters | created_at | updated_at
----+---------+------+----------+------+--------+---------+------------+-----+-----+------+------------------+------------+------------
(0 rows)

=> \d polls_id_seq
        Sequence "public.polls_id_seq"
    Column | Type | Value
---------------+---------+---------------------
 sequence_name | name | polls_id_seq
 last_value | bigint | 4524
 start_value | bigint | 1
 increment_by | bigint | 1
 max_value | bigint | 9223372036854775807
 min_value | bigint | 1
 cache_value | bigint | 1
 log_cnt | bigint | 24
 is_cycled | boolean | f
 is_called | boolean | t
Owned by: public.polls.id

=> select count(id),max(id) from polls;
 count | max
-------+-----
     0 |
(1 row)

```

Can you, please, help with this issue.  
As because of this error we cannot update Discourse forum.

---

<div class="post-metadata">

### Author: ![AlexMl](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@AlexMl](https://meta.discourse.org/u/AlexMl)
#### Post date: [December 10, 2018, 8:28am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/2 "2018-12-10T08:28:42Z")

</div>

Hi,

Can someone from development team take a look to this question ?  
We are blocked with update of Discourse forum to the latest version, because of this error.  
And also we cannot install additional plugin, as docker container is not compiled and as a result is not running.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [December 10, 2018, 3:38pm UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/4 "2018-12-10T15:38:33Z")

</div>

The error indicates that the post (id: `662593`) has more than one polls named “`poll`”.

Can you share the raw of that post? You can find it by going to `https://your.discourse/posts/662593/raw`

Also, can you share the results of this command when ran in the rails console?

```rails
PostCustomField.where(post_id: 662593, name: "polls")

```

---

<div class="post-metadata">

### Author: ![AlexMl](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@AlexMl](https://meta.discourse.org/u/AlexMl)
#### Post date: [December 11, 2018, 11:52am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/5 "2018-12-11T11:52:24Z")

</div>

Unfortunately, I cannot share the raw of post from [https://your.discourse/posts/662593/raw](https://your.discourse/posts/662593/raw) , as it is not running now because of error.

The output from rail`s console:

```
[1] pry(main)> PostCustomField.where(post_id: 662593, name: "polls")
=> [#<PostCustomField:0x00005641ffca5f48
  id: 376790,
  post_id: 662593,
  name: "polls",
  value:
   "{\"poll\":{\"options\":[{\"id\":\"4d8a15e3cc35750f016ce15a43937620\",\"html\":\"1\",\"votes\":1,\"voter_ids\":[9160]},{\"id\":\"cd314db7dfbac2b10687b6f39abfdf41\",\"html\":\"2\",\"votes\":0,\"voter_ids\":[]}],\"voters\":1,\"status\":\"open\",\"type\":\"regular\",\"public\":\"true\",\"name\":\"poll\"},\"\":{\"options\":[{\"id\":\"4d8a15e3cc35750f016ce15a43937620\",\"html\":\"1\",\"votes\":0},{\"id\":\"cd314db7dfbac2b10687b6f39abfdf41\",\"html\":\"2\",\"votes\":0}],\"voters\":0,\"status\":\"open\",\"type\":\"regular\",\"public\":\"true\"}}",
  created_at: Thu, 26 Apr 2018 01:56:00 UTC +00:00,
  updated_at: Thu, 26 Apr 2018 01:56:00 UTC +00:00>]

```

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [December 11, 2018, 11:57am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/6 "2018-12-11T11:57:28Z")

</div>

🤔 the data looks fine.

Can you try to clean all the polls related tables and then try to run the migration again?

---

<div class="post-metadata">

### Author: ![AlexMl](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@AlexMl](https://meta.discourse.org/u/AlexMl)
#### Post date: [December 11, 2018, 2:43pm UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/7 "2018-12-11T14:43:10Z")

</div>

There are 3 poll related tables.  
I tried to delete rows, but these tables are empty.

```
=> DELETE FROM poll_options;
DELETE 0
=> DELETE FROM poll_votes;
DELETE 0
=> DELETE FROM polls;
DELETE 0

```

Then ran the migration again, but it failed with the same error as earlier.

---

<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: [December 20, 2018, 10:13pm UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/8 "2018-12-20T22:13:19Z")

</div>

It turns out that the problem was that the poll had been duplicated in a quote block, and the migration didn’t know to ignore it. It was in a PM, apparently as an example of how polls worked. I deleted the duplicate and the migration ran fine.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [December 21, 2018, 7:51am UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/9 "2018-12-21T07:51:24Z")

</div>

> [@pfaffman](#):
>
> the poll had been duplicated in a quote block

Weird, I’ll have to test that…

---

<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: [November 14, 2021, 4:14pm UTC](https://meta.discourse.org/t/update-failed-pg-error/103684/10 "2021-11-14T16:14:38Z")

</div>


