14
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Ruby on Rails/PostgreSQL】"PG::Error: ERROR: duplicate key value violates unique constraint"と出てレコード作成できない時の対処法

Posted at

今回の問題

  • Rails/Active Recordで新しいレコードを作成しようとすると、エラーが出てしまう
PG::Error: ERROR:  duplicate key value violates unique constraint "addresses_pkey"
DETAIL:  Key (id)=(2) already exists.

原因

ActiveRecordでレコードを作成するとき、primary_keyがidになっている時はAddress.create(name: "toyota")とidを指定せずに作成しても自動でidを付与してくれる。シーケンスも自動インクリメントしてくれる。

ただし、idの順序がすでに指定されてある場合などにAddress.create(id: 12, name: "suzuki")とidを指定しレコード作成した場合、次のレコード作成時にシーケンスが自動インクリメントされない。そのため、DETAIL: Key (id)=(2) already exists.というエラーが出てしまう。

解決方法

SQLを発行して、シーケンスを訂正する。下記のSQLを実行すると、テーブル内のレコードからidの最大値+1した値をシーケンスとして保存してくれる。

# Addressテーブルのidのシーケンスを直す場合の例
SELECT setval('addresses_id_seq', coalesce((SELECT MAX(id)+1 FROM addresses), 1), false)

Railsアプリ内上記SQLを実行する場合は下記のように書く

ActiveRecord::Base.connection.execute("SELECT setval('addresses_id_seq', coalesce((SELECT MAX(id)+1 FROM addresses), 1), false)")

参考サイト

14
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?