4

「position」列を持つ「items」テーブルがあります。positionには、一意でnullではない制約があります。位置xに新しい行を挿入するには、最初に後続のアイテムの位置をインクリメントしてみます。

UPDATE items SET position = position + 1 WHERE position >= x;

これにより、一意の制約違反が発生します。

ERROR:  duplicate key value violates unique constraint

問題は、PostgreSQLが更新を実行する順序にある​​ようです。PostgreSQL <9.0の固有の制約は延期できず、残念ながら9.0を使用することは現在のところオプションではありません。また、UPDATEステートメントはORDER BY句をサポートしておらず、以下も機能しません(キー違反が重複しています)。

UPDATE items SET position = position + 1 WHERE id IN (
  SELECT id FROM items WHERE position >= x ORDER BY position DESC)

コード内のすべてのアイテムを反復処理する必要のないソリューションを誰かが知っていますか?

4

4 に答える 4

3

複数の一意のインデックスを持つ別のテーブル:

create table utest(id integer, position integer not null, unique(id, position));
test=# \d utest
      Table "public.utest"
  Column  |  Type   | Modifiers 
----------+---------+-----------
 id       | integer | 
 position | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id, "position")

いくつかのデータ:

insert into utest(id, position) select generate_series(1,3), 1;
insert into utest(id, position) select generate_series(1,3), 2;
insert into utest(id, position) select generate_series(1,3), 3;

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        2
  1 |        3
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

適切な順序で位置の値を更新するプロシージャを作成しました。

create or replace function update_positions(i integer, p integer) 
  returns void as $$
declare
  temprec record;
begin
  for temprec in 
    select * 
      from utest u 
      where id = i and position >= p 
      order by position desc 
  loop
    raise notice 'Id = [%], Moving % to %', 
      i, 
      temprec.position, 
      temprec.position+1;

    update utest 
      set position = position+1 
      where position=temprec.position and id = i;
  end loop;
end;
$$ language plpgsql;

いくつかのテスト:

test=# select * from update_positions(1, 2);
NOTICE:  Id = [1], Moving 3 to 4
NOTICE:  Id = [1], Moving 2 to 3
 update_positions 
------------------

(1 row)

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        3
  1 |        4
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

それが役に立てば幸い。

于 2010-10-24T10:18:49.020 に答える
2


PostgreSQLはトランザクションDDLのフルセットをサポートしているため、次のようなことを簡単に行うことができます。

create table utest(id integer unique not null);
insert into utest(id) select generate_series(1,4);

テーブルは次のようになります。

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  1
  2
  3
  4
(4 rows)

そして今、全体の魔法:

begin;
alter table utest drop constraint utest_id_key;
update utest set id = id + 1;
alter table utest add constraint utest_id_key unique(id);
commit;

その後、次のようになります。

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  2
  3
  4
  5
(4 rows)

このソリューションには1つの欠点があります。テーブル全体をロックする必要がありますが、ここでは問題にならない可能性があります。

于 2010-10-23T23:10:46.200 に答える
0

「より正しい」解決策は、制約を作成することかもしれませんDEFERRABLE

ALTER TABLE channels ADD CONSTRAINT 
channels_position_unique unique("position") 
DEFERRABLE INITIALLY IMMEDIATE

次に、インクリメントするときにその制約をDEFERREDに設定し、完了したらIMMEDIATEに戻します。

SET CONSTRAINTS channels_position_unique DEFERRED;
UPDATE channels SET position = position+1 
WHERE position BETWEEN 1 AND 10;
SET CONSTRAINTS channels_position_unique IMMEDIATE;
于 2016-02-11T13:39:18.977 に答える
0

テーブルとドロップの制約を変更しないバリアント:

UPDATE items t1 
SET    position = t2.position + 1 
FROM   (SELECT position
    FROM   items 
    ORDER  BY position DESC) t2 
WHERE t2.position >= x AND t1.position = t2.position

オンラインの例: http: //rextester.com/FAU54991

于 2018-09-19T08:24:08.233 に答える