在更新非唯一字段期间,“重复键值违反唯一约束”

时间:2017-06-05 13:19:10

标签: postgresql

我们正在尝试将我们的应用程序移动到新的postgresql集群。

在执行此操作时,我们注意到应用程序抛出异常:

[2017-06-02 14:43:34,530] ........ (psycopg2.IntegrityError) duplicate key value violates unique constraint "items_url"
DETAIL:  Key (url)=(http://www.domainname.ru/ap_module/content/article/400-professional/140-professional/11880) already exists.
 [SQL: 'UPDATE items SET status=%(status)s WHERE items.id IN ....

这很奇怪,因为:

  • 应用程序写入items字段,而不是items_urlitems_urlitems的索引,实际上是
  • UPDATE仅更改未标记unique的状态字段,而且不是primary key

表项目:

id              | integer                     | not null default nextval(('public.items_id_seq'::text)::regclass)
 ctime           | timestamp without time zone | not null default now()
 pubdate         | timestamp without time zone | not null default now()
 resource_id     | integer                     | not null default 0
 url             | text                        |
 title           | text                        |
 description     | text                        |
 body            | text                        |
 status          | smallint                    | not null default 0
 image           | text                        |
 orig_id         | integer                     | not null default 0
 mtime           | timestamp without time zone | not null default now()
 checksum        | text                        |
 video_url       | text                        |
 audio_url       | text                        |
 content_type    | smallint                    | default 0
 author          | text                        |
 video           | text                        |
 fulltext_status | smallint                    | default 0
 summary         | text                        |
 image_id        | integer                     |
 video_id        | integer                     |
 priority        | smallint                    |

Indexes:
    "items_pkey" PRIMARY KEY, btree (id)
    "items_url" UNIQUE, btree (url)
    "items_resource_id" btree (resource_id)
    "ndx__items__ctime" btree (ctime)
    "ndx__items__image" btree (image_id)
    "ndx__items__mtime" btree (mtime)
    "ndx__items__pubdate" btree (pubdate)
    "ndx__items__video" btree (video_id)
Foreign-key constraints:
    "items_fkey1" FOREIGN KEY (image_id) REFERENCES images(id) ON UPDATE CASCADE ON DELETE SET NULL
    "items_fkey2" FOREIGN KEY (video_id) REFERENCES videos(id) ON UPDATE CASCADE ON DELETE SET NUL

嗯,问题是为什么会发生这种情况,我该如何解决这个问题呢? 谢谢。

UPD1: 我尝试在9.4上重现它。 - 转载

使用client_encoding播放。在任何地方编码都是一样的。

0 个答案:

没有答案