Knowledgebase

Auto-Increment Problems Print

  • databasesphpmyadmin, databases, troubleshooting, restore, woocommerce, errors, guide, howto
  • 0

When new rows fail or get wrong ids.

WHAT AUTO-INCREMENT IS

The counter assigning the next id when a row is inserted.

THE COMMON PROBLEM

After a partial restore or a manual insert, the counter can be lower than the highest existing id.

The next insert then attempts an id that already exists, producing a duplicate key error.

THE SYMPTOM

Duplicate entry errors on the primary key, when inserting new content, for no apparent reason.

THE FIX

Set the auto-increment value above the highest existing id.

The Operations tab for the table has an AUTO_INCREMENT field.

Find the highest id first:

SELECT MAX(id) FROM table;

Set the counter to at least one above it.

THE OTHER CASE

Gaps in ids. These are normal and harmless: deleted rows leave gaps, and failed inserts consume numbers.

Do not attempt to renumber to close gaps. Other tables reference those ids.

AFTER A RESTORE

Check this if inserts start failing immediately afterwards.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot