ActiveRecord::RecordNotUnique: duplicate key value Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Rails: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"

A unique index rejected an insert/update because the value already exists.

Exact message you may see ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"Mysql2::Error: Duplicate entry 'a@b.com' for key 'index_users_on_email'ActiveRecord::RecordNotUnique (PG::UniqueViolation)SQLite3::ConstraintException: UNIQUE constraint failed: users.emailKey (id)=(1) already exists
Seen on: Ruby PostgreSQL

Meaning

Validations (validates :email, uniqueness: true) don't prevent races; the DB constraint does. Rescue RecordNotUnique, use find_or_create_by inside a retry, or upsert. Also check sequences out of sync after data imports (PG::UniqueViolation ... Key (id)=(1)).

Common causes

  • Duplicate email/slug/code
  • Race between validation and insert
  • Primary key sequence behind max(id) after import
  • Seeds run twice
  • Retry causing double insert

⚡ Quick fix

  1. begin; ...; rescue ActiveRecord::RecordNotUnique; retry; end or upsert
  2. Reset sequences: ActiveRecord::Base.connection.reset_pk_sequence!("users")
  3. Keep a unique index plus validation

Detailed fix by platform

Rails

  1. User.upsert({ email: e, name: n }, unique_by: :email)

How to diagnose

  1. Index — Which?
  2. Race — Concurrent?
  3. Sequence — In sync?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

1022 — MySQL ERROR 1022 (23000): Can't write; duplicate key in table (MySQL)10221048 — MySQL ERROR 1048 (23000): Column 'x' cannot be null (MySQL)10481062 — MySQL ERROR 1062 (23000): Duplicate entry for key (MySQL)10621451 — MySQL ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (MySQL)14511452 — MySQL ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (MySQL)145223502 — ERROR: null value in column "x" of relation "y" violates not-null constraint (PostgreSQL)2350223503 — ERROR: insert or update on table "x" violates foreign key constraint "fk" — Key (user_id)=(5) is not present in table "users" (PostgreSQL)2350323503 parent — ERROR: update or delete on table "users" violates foreign key constraint on table "orders" — Key (id)=(5) is still referenced (PostgreSQL)23503 parentActiveRec…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.