Getting `MySQL error 1062: Duplicate entry '...' for key 'PRIMARY'` When I try to duplicate a record

When trying to duplicate a record I get this message in the browser

Database failure. Please refer to suitecrm.log for details.

In suitecrm.log I get this message

Thu Jun 6 21:15:27 2024 [25701][6468507c-367f-601c-60e9-6055259c7428][FATAL] Mysqli_query failed.
Thu Jun 6 21:15:27 2024 [25701][6468507c-367f-601c-60e9-6055259c7428][FATAL] Error inserting into table: some_module_name: Query Failed: INSERT INTO some_module_name (id,name,date_entered,date_modified,modified_user_id,created_by,deleted,code,element, ...)
VALUES ('9bf78914-2875-ff17-8831-66626d393c0a','10000048','2024-06-07 02:15:27','2024-06-07 02:15:27','6468507c-367f-601c-60e9-6055259c7428','6468507c-367f-601c-60e9-6055259c7428',0,'10000048','Person', ...): MySQL error 1062: Duplicate entry '9bf78914-2875-ff17-8831-66626d393c0a' for key 'PRIMARY'
Thu Jun 6 21:15:27 2024 [25701][6468507c-367f-601c-60e9-6055259c7428][FATAL] Exception handling in /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php:400
Thu Jun 6 21:15:27 2024 [25701][6468507c-367f-601c-60e9-6055259c7428][FATAL] Exception in Controller: Database error. Please see SuiteCRM error .log for more details.
Thu Jun 6 21:15:27 2024 [25701][6468507c-367f-601c-60e9-6055259c7428][FATAL] backtrace:
#0 /host/projects/suitecrm-workspace/crm/app/crm/include/database/DBManager.php(353): sugar_die()
#1 /host/projects/suitecrm-workspace/crm/app/crm/include/database/DBManager.php(328): DBManager->registerError()
#2 /host/projects/suitecrm-workspace/crm/app/crm/include/database/MysqliManager.php(179): DBManager->checkError()
#3 /host/projects/suitecrm-workspace/crm/app/crm/include/database/DBManager.php(519): MysqliManager->query()
#4 /host/projects/suitecrm-workspace/crm/app/crm/data/SugarBean.php(2426): DBManager->insert()
#5 /host/projects/suitecrm-workspace/crm/app/crm/custom/_pw/extensions/logic_hooks/Custom_LogicHooks.php(152): SugarBean->save()
#6 /host/projects/suitecrm-workspace/crm/app/crm/include/utils/LogicHook.php(272): Custom_LogicHooks->after_relationship_delete()
#7 /host/projects/suitecrm-workspace/crm/app/crm/include/utils/LogicHook.php(208): LogicHook->process_hooks()
#8 /host/projects/suitecrm-workspace/crm/app/crm/data/SugarBean.php(3129): LogicHook->call_custom_logic()
#9 /host/projects/suitecrm-workspace/crm/app/crm/data/Relationships/SugarRelationship.php(431): SugarBean->call_custom_logic()
#10 /host/projects/suitecrm-workspace/crm/app/crm/data/Relationships/One2MBeanRelationship.php(172): SugarRelationship->callAfterDelete()
#11 /host/projects/suitecrm-workspace/crm/app/crm/data/Relationships/One2MBeanRelationship.php(86): One2MBeanRelationship->remove()
#12 /host/projects/suitecrm-workspace/crm/app/crm/data/Link2.php(549): One2MBeanRelationship->add()
#13 /host/projects/suitecrm-workspace/crm/app/crm/custom/_pw/extensions/utils/Custom_Utils.php(669): Link2->add()
#14 /host/projects/suitecrm-workspace/crm/app/crm/custom/_pw/extensions/logic_hooks/Custom_LogicHooks.php(53): Custom_Utils::duplicate_relations()
#15 /host/projects/suitecrm-workspace/crm/app/crm/include/utils/LogicHook.php(272): Custom_LogicHooks->after_save()
#16 /host/projects/suitecrm-workspace/crm/app/crm/include/utils/LogicHook.php(208): LogicHook->process_hooks()
#17 /host/projects/suitecrm-workspace/crm/app/crm/data/SugarBean.php(3129): LogicHook->call_custom_logic()
#18 /host/projects/suitecrm-workspace/crm/app/crm/data/SugarBean.php(2442): SugarBean->call_custom_logic()
#19 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php(684): SugarBean->save()
#20 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php(525): SugarController->action_save()
#21 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php(494): SugarController->do_action()
#22 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php(468): SugarController->handle_action()
#23 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/Controller/SugarController.php(373): SugarController->process()
#24 /host/projects/suitecrm-workspace/crm/app/crm/include/MVC/SugarApplication.php(113): SugarController->execute()
#25 /host/projects/suitecrm-workspace/crm/app/crm/index.php(52): SugarApplication->execute()
#26 {main}

The id 99c8bb4a-a8fe-9173-eb45-66622cc7335a actually exists and is the cause of the error

The suite version is 7.11.3

This happens when trying to duplicate any module

This message gives you some clue as to the cause of this error?

Regards and thanks in advance

I was able to identify the reason for the error. This happens because of a logic defined in the hook after_save. In this logic, an attempt is made to duplicate the relations of the duplicate record. At one point when an attempt is made to $bean->$name_relation->add($relatedBean['id']); the $bean is saved and the error described is triggered.

I was able to resolve this by avoiding duplicates

foreach ($relationship as $relatedBean) {
    // Check if a relation already exists to avoid duplicates
    $existingRelationship = $bean->$name_relation->getBeans(['where' => "id = '{$relatedBean['id']}'"]);
    if (empty($existingRelationship)) {
        // Add the relation to the current bean
        $bean->$name_relation->add($relatedBean['id']);
    }
}

My question is, does it make sense to duplicate the relationships of a duplicate item in the context of suitecrm?

I ask this because this logic is very old, it exists without comment explaining its functionality.

Isn’t the answer different, according to the relationship type?

I mean, if it is a one-to-one relationship, there shouldn’t be any more relatioships, but if it is one-to-many, then there might be (in one direction), and if it’s many-to-many, then in both directions it should be ok.

@pgr Thank you very much for answering. Your comment helps me. I understand that when duplicating a record in the relationship type it would be automatically duplicated if necessary.

1 Like