Webform Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

Submitted by sysop on Sun, 08/20/2023 - 16:45

This issue "Webform Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry" arises when there are multiple ids for the same webforms are created. Here are the ways to fix the issue:

1. If you are not customizing the next serial number, you could purge the webform table MySQL and it will rebuild itself:

DELETE FROM webform;

2. You can also run the below drush command to fix the issue:

drush sqlq "DELETE FROM webform;"

3. When this happens during the mysqldump restore and nothing works. You can edit the mysqldump file and remove the primary key itself from the table. Or you can also execute the below command:

Alter table webform DROP PRIMARY KEY next_serial;

Note: Kindly remember that dropping the primary key is a dirty hack. Kindly use this step3 with caution!!!! Use this only when you are desperate.