Duplicate entry for key 'PRIMARY' on creating new vitals

Hi all,

I have imported about 19000 CCD’s successfully without any issue.

However, i am unable to create any new vitals for either new or existing patients. I am able to make an update but cannot create new vitals.

Please see the error below in the logs:

PHP Fatal error:  Uncaught OpenEMR\\Common\\Database\\SqlQueryException: Failed to execute statement. Error: Duplicate entry '19203' for key 'PRIMARY' Statement: INSERT INTO form_vitals SET `id` = ?,`uuid` = ?,`date` = ?,`pid` = ?,`user` = ?,`groupname` = ?,`activity` = ?,`bps` = ?,`bpd` = ?,`weight` = ?,`height` = ?,`temperature` = ?,`temp_method` = ?,`pulse` = ?,`respiration` = ?,`note` = ?,`BMI` = ?,`BMI_status` = ?,`waist_circ` = ?,`head_circ` = ?,`oxygen_saturation` = ?,`oxygen_flow_rate` = ?,`ped_weight_height` = ?,`ped_bmi` = ?,`ped_head_circ` = ?,`inhaled_oxygen_concentration` = ? in /var/www/html/mbma/library/sql.inc.php:164\nStack trace:\n#0 /var/www/html/mbma/src/Common/Database/QueryUtils.php(162): sqlStatementThrowException()\n#1 /var/www/html/mbma/src/Services/VitalsService.php(379): OpenEMR\\Common\\Database\\QueryUtils::sqlStatementThrowException()\n#2 /var/www/html/mbma/src/Services/VitalsService.php(403): OpenEMR\\Services\\VitalsService->saveVitalsArray()\n#3 /var/www/html/mbma/interface/forms/vitals/C_FormVitals.class.php(439): OpenEMR\\Services\\VitalsService->saveVitalsForm()\n#4 /var/www/html/mbma/interface/forms/vitals/save.php(24): C_FormVitals->default_action_process()\n#5 {main}\n  thrown in /var/www/html/mbma/library/sql.inc.php on line 164, referer: 

i checked the auto_increment value of the form_vitals table and it was 23422. So i do not know where that duplicate error is coming from.

Can anyone point in the right direction?

Open form_vitals in phpMyAdmin and sort id and report lowest and highest id.
also @kkappiah run this query:

SELECT id, COUNT()
FROM form_vitals
GROUP BY id
HAVING COUNT(
) > 1;

I’m running mysql from the command line.

here’s the minimum value = 8
The maximum value = 23421

I used the min and max functions for the id

When i executed the query, i had an error, because COUNT() was expecting a parameter. I passed '’ like so COUNT() and it returned an empty set.

I also passed the id like so COUNT(id) and it returned an empty set as well

Sorry, I put query together off top of my head.
Still weird. The only other way I can think of is if vital save is using an id for save but I don’t see how because that code is pretty solid.
I’ve seen this before on other tables but not lately. I just don’t remember. Seems it had something to do with the table but the error descript was misleading.

Can you debug?
like in VitalsService.php around line 379 to check the $values[id] should be null.

Also check forms table for the new vital forms and look at the form_ids. Form is saved in forms table before form_vitals so should have invalid forms recorded.

VitalsService.php around line 379

$values['id'] is empty.

When i check form_vitals and forms tables, i do not see any data matching today’s date. I’ll try again tomorrow and update you.

I’ve recreated but still haven’t a clue yet!
[21-Feb-2025 18:20:51 America/New_York] PHP Fatal error: Uncaught OpenEMR\Common\Database\SqlQueryException: Failed to execute statement. Error: Duplicate entry '1055' for key 'PRIMARY' Statement: INSERT INTO form_vitals SETid = ?,uuid = ?,date = ?,pid = ?,user = ?,groupname = ?,activity = ?,bps = ?,bpd = ?,height = ?,weight = ?,temperature = ?,temp_method = ?,pulse = ?,respiration = ?,note = ?,BMI = ?,BMI_status = ?,waist_circ = ?,head_circ = ?,oxygen_saturation = ?,oxygen_flow_rate = ?,ped_weight_height = ?,ped_bmi = ?,ped_head_circ = ?,inhaled_oxygen_concentration= ? in C:\xampp\htdocs\openemr\library\sql.inc.php:164 Stack trace: #0 C:\xampp\htdocs\openemr\src\Common\Database\QueryUtils.php(162): sqlStatementThrowException('INSERT INTO for...', Array)

`
$value = Array
(
[0] => 1055
[1] => �D����@�=����s’
[2] => 20250221181839
[3] => 61
[4] => Administrator
[5] => Default
[6] => 1
[7] =>
[8] =>
[9] =>
[10] => 177
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
)

`

@kkappiah
OpenEMR form_vital insert uses the genID value from the sequence table. I don’t on CCDA vitals import and use the autoincrement so when creating new vitals in openemr it is using the sequence value and not the max(id) in form_vitals thus duplicate id’s.

Recommend you get current max id in form_vital and set sequence table to max(id) + 1
Then you should be okay.

I don’t know if I’ll fix in import or openemr. openemr makes the most sense!

Let me know.

Hello Jerry, i got it to work based on your instructions.

Many thanks