How to fix ERROR 1061: Duplicate key name in MySQL

MySQLINTERMEDIATEMEDIUMUpdated Dec 22, 2025ยท 303 views

This error occurs when you try to create a table, index, or constraint with a name that already exists in the table. Common causes include running migrations twice or conflicting auto-generated index names. Fix it by checking existing indexes and either dropping the duplicate or using IF NOT EXISTS syntax in MySQL 8.0+.

Quick fix

Fixes ERROR 1061: Duplicate key name

ERROR 1061 (42000): Duplicate key name 'index_name'
  1. Identify the duplicate key name from the error message
  2. Check existing indexes on the table using SHOW INDEX
  3. Drop the duplicate index if it is unnecessary
  4. Use IF NOT EXISTS syntax for idempotent migrations (MySQL 8.0+)
mysql โ€” error
ERROR 1061 (42000): Duplicate key name 'index_name'
ERROR 1061: Duplicate key name

What this error means

ERROR 1061 (ER_DUP_KEYNAME) occurs when MySQL encounters a CREATE TABLE, ALTER TABLE, or CREATE INDEX statement that attempts to create an index or constraint with a name that already exists in the table. MySQL requires that each index and constraint name be unique within a table. This typically happens during database migrations, schema updates, or when multiple development branches introduce conflicting index definitions. When this error occurs, MySQL rolls back the entire statement, leaving the table structure unchanged until you resolve the duplicate.

Typical symptoms

  • Migration fails with "ERROR 1061: Duplicate key name" message
  • CREATE TABLE or ALTER TABLE statements are rejected
  • Database deployment or schema update process halts
  • Running the same migration script twice causes the error
  • ORM or migration tool produces duplicate index creation attempts

Common causes

  1. Running the same migration script or schema update multiple times without checking if indexes already exist
  2. Parallel feature branches introducing index definitions with identical names that get merged together
  3. ORM tools (like Prisma, TypeORM, or SQLAlchemy) auto-generating index names that conflict with manually defined indexes
  4. Foreign key constraints creating implicit indexes that clash with explicitly defined indexes on the same column
  5. Renaming a column without properly dropping and recreating its associated indexes
  6. Case-insensitive database configurations treating different case variations of the same name as duplicates

How to fix "ERROR 1061: Duplicate key name"

1Identify the duplicate key name from the error message

The error message includes the name of the duplicate key. Look for the key name in parentheses in the error output:

bash
ERROR 1061 (42000): Duplicate key name 'index_name'

Note the exact name of the problematic index or key.

2Check existing indexes on the table using SHOW INDEX

Connect to your MySQL database and run the SHOW INDEX command to list all existing indexes:

sql
SHOW INDEX FROM table_name;

Replace table_name with the actual table name from your error. This will display all current indexes, their columns, and their names. Look for the duplicate key name in the Key_name column.

3Drop the duplicate index if it is unnecessary

If you identify that the index is redundant or was created by mistake, drop it using ALTER TABLE:

sql
ALTER TABLE table_name DROP INDEX index_name;

Replace table_name with your table name and index_name with the name of the duplicate index. This removes the conflicting index so your migration can proceed.

4Use IF NOT EXISTS syntax for idempotent migrations (MySQL 8.0+)

To prevent this error in the future, use the IF NOT EXISTS clause in your CREATE INDEX statements:

sql
CREATE INDEX IF NOT EXISTS index_name ON table_name (column_name);

This syntax tells MySQL to only create the index if it does not already exist, making your migrations safe to run multiple times. This is the recommended approach for modern MySQL versions.

5Retry the failing migration or schema update

After resolving the duplicate index, retry the operation that originally failed:

sql
-- Your original CREATE TABLE or ALTER TABLE statement
ALTER TABLE table_name ADD INDEX index_name (column_name);

If you modified the index name in your script, ensure the new name is unique. Test the migration in a development environment before applying it to production.

Advanced notes

In MySQL 8.0 and later, the IF NOT EXISTS and IF NOT ERROR clauses make migrations idempotent, allowing you to safely rerun them without encountering duplicate key errors. On case-insensitive file systems or with case-insensitive MySQL configurations, index names that differ only in case may still be treated as duplicates. Always use lowercase or consistent casing for index names. Some storage engines like Aria may have stricter index naming rules. If this error occurs during foreign key creation, ensure that automatic indexes created by the foreign key constraint do not conflict with manually defined indexes on the same columns. When using ORM frameworks, check their migration generation logic to ensure they use IF NOT EXISTS syntax or implement conflict detection.

Frequently asked questions

What does "ERROR 1061: Duplicate key name" mean?
ERROR 1061 (ER_DUP_KEYNAME) occurs when MySQL encounters a CREATE TABLE, ALTER TABLE, or CREATE INDEX statement that attempts to create an index or constraint with a name that already exists in the table. MySQL requires that each index and constraint name be unique within a table. This typically happens during database migrations, schema updates, or when multiple development branches introduce conflicting index definitions.
How do I fix "ERROR 1061: Duplicate key name"?
This error occurs when you try to create a table, index, or constraint with a name that already exists in the table. Common causes include running migrations twice or conflicting auto-generated index names. Fix it by checking existing indexes and either dropping the duplicate or using IF NOT EXISTS syntax in MySQL 8.0+.
What causes "ERROR 1061: Duplicate key name"?
Running the same migration script or schema update multiple times without checking if indexes already exist Parallel feature branches introducing index definitions with identical names that get merged together ORM tools (like Prisma, TypeORM, or SQLAlchemy) auto-generating index names that conflict with manually defined indexes
Is the "ERROR 1061: Duplicate key name" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading