# Rails: ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "status" of relation "orders" already exists

**Code:** `PG::DuplicateColumn / Duplicate column name (migration)` · **Category:** Ruby / Rails · **Seen on:** Ruby, PostgreSQL

A migration tried to add a column/table/index that already exists.

## Meaning

Usually a migration was run partially, run manually, or merged from two branches. Check the schema (`\d orders`), `schema_migrations` entries, and make migrations idempotent (`add_column ..., if_not_exists: true`, `column_exists?`). Rolling back and re-running can fix half-applied migrations (PostgreSQL DDL is transactional, MySQL isn't).

## Common causes

- Migration applied partially or manually
- Two branches adding the same column
- Schema loaded then migrations rerun
- MySQL non-transactional DDL left half-done
- Duplicate migration files

## Quick fix

1. `add_column :orders, :status, :string, if_not_exists: true`
2. Inspect `schema_migrations` and the table
3. Remove the duplicate migration/file or fix the version

## Detailed fix by platform

### Rails

1. `bin/rails db:migrate:status | tail; bin/rails runner 'p Order.column_names'`

## How to diagnose

1. **Column** — Exists?
2. **Version** — In schema_migrations?
3. **Branches** — Duplicate?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ruby/activerecord-pg-duplicatecolumn-migration)
