Answers.Body: <p>According to this:</p>
  <pre><code>Cannot add or update a child row: a foreign key constraint fails
  (`xxx.xxx`.`civicrm_activity`, CONSTRAINT `civicrm_activity_ibfk_3` 
  FOREIGN KEY (`parent_id`) REFERENCES `civicrm_activity_fudge` 
  (`id`) ON DELETE SET NULL)]
  </code></pre>
  <p>you may want to check the <code>civicrm_activity</code> table structure, discover where the <code>civicrm_activity_fudge</code> has come from and edit or remove this reference.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-15T11:03:04.010Z
Answers.LastActivityDate: 2023-12-15T11:03:04.010Z
Answers.UserDisplayName: yurg
Answers.Body: <p>Run below query</p>
  <pre><code>ALTER TABLE civicrm_activity_contact DROP FOREIGN KEY civicrm_activity_ibfk_3;
  
  ALTER TABLE civicrm_activity_contact 
  ADD CONSTRAINT `FK_civicrm_activity_contact_activity_id` FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE;
  </code></pre>
  <p>Note: Take db backup before running query</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-15T11:50:25.637Z
Answers.LastActivityDate: 2023-12-15T11:50:25.637Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>When merging 2 contacts this error appears however when the contact is checked the merge has succeeded:</p>
  <pre><code>$Fatal Error Details = array:3 [
    &quot;message&quot; =&gt; &quot;DB Error: constraint violation&quot;
    &quot;code&quot; =&gt; null
    &quot;exception&quot; =&gt; CRM_Core_Exception {#1782
      #message: &quot;DB Error: constraint violation&quot;
      #code: -3
      #file: &quot;/var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/api/api.php&quot;
      #line: 138
      #cause: null
      -_trace: null
      -errorData: array:8 [
        &quot;error_code&quot; =&gt; -3
        &quot;sql&quot; =&gt; &quot;INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 ) &quot;
        &quot;debug_info&quot; =&gt; &quot;INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`xxx.xxx`.`civicrm_activity`, CONSTRAINT `civicrm_activity_ibfk_3` FOREIGN KEY (`parent_id`) REFERENCES `civicrm_activity_fudge` (`id`) ON DELETE SET NULL)]&quot;
        &quot;entity&quot; =&gt; &quot;Activity&quot;
        &quot;action&quot; =&gt; &quot;create&quot;
        &quot;is_error&quot; =&gt; 1
        &quot;error_message&quot; =&gt; &quot;DB Error: constraint violation&quot;
        &quot;debug_information&quot; =&gt; &quot;INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 ) &quot;
      ]
      trace: {
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/api/api.php:138 {
          civicrm_api3(string $entity, string $action, array $params = [])
          › if (is_array($result) &amp;&amp; !empty($result['is_error'])) {
          ›   throw new CRM_Core_Exception($result['error_message'], $result['error_code'] ?? 'undefined', $result);
          › }
        }
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Dedupe/Merger.php:2188 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Dedupe/Merger.php:2104 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Contact/Form/Merge.php:324 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Form.php:622 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/StateMachine.php:144 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/QuickForm/Action/Next.php:43 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/packages/HTML/QuickForm/Controller.php:203 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/packages/HTML/QuickForm/Page.php:103 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Controller.php:355 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Utils/Wrapper.php:98 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:295 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:69 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:36 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm.php:121 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm.php:40 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Dispatcher/LegacyComponentDispatcher.php:71 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Dispatcher/LegacyComponentDispatcher.php:73 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Component/ComponentHelper.php:361 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/AdministratorApplication.php:143 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/AdministratorApplication.php:186 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/CMSApplication.php:293 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/includes/app.php:61 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/index.php:32 { …}
  </code></pre>
  <p>Running CiviCRM 5.68 , PHP 8.1 , Joomla 4.4.1</p>
  
Tags: <contacts><duplicate-contacts>
Title: Merge fails with DB Constraint violation
ThreadText: # Merge fails with DB Constraint violation
  
  (Asked by: username_0 on 2023-12-14)
  
  When merging 2 contacts this error appears however when the contact is checked the merge has succeeded:
  $Fatal Error Details = array:3 [
    "message" => "DB Error: constraint violation"
    "code" => null
    "exception" => CRM_Core_Exception {#1782
      #message: "DB Error: constraint violation"
      #code: -3
      #file: "/var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/api/api.php"
      #line: 138
      #cause: null
      -_trace: null
      -errorData: array:8 [
        "error_code" => -3
        "sql" => "INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 ) "
        "debug_info" => "INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`xxx.xxx`.`civicrm_activity`, CONSTRAINT `civicrm_activity_ibfk_3` FOREIGN KEY (`parent_id`) REFERENCES `civicrm_activity_fudge` (`id`) ON DELETE SET NULL)]"
        "entity" => "Activity"
        "action" => "create"
        "is_error" => 1
        "error_message" => "DB Error: constraint violation"
        "debug_information" => "INSERT INTO `civicrm_activity` (`activity_type_id` , `subject` , `activity_date_time` , `status_id` , `priority_id` , `parent_id` ) VALUES ( 54 , 'Contact ID 11170 has been merged into Contact ID 41737 and deleted.' ,  20231214131919 ,  2 ,  2 ,  202919 ) "
      ]
      trace: {
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/api/api.php:138 {
          civicrm_api3(string $entity, string $action, array $params = [])
          › if (is_array($result) && !empty($result['is_error'])) {
          ›   throw new CRM_Core_Exception($result['error_message'], $result['error_code'] ?? 'undefined', $result);
          › }
        }
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Dedupe/Merger.php:2188 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Dedupe/Merger.php:2104 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Contact/Form/Merge.php:324 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Form.php:622 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/StateMachine.php:144 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/QuickForm/Action/Next.php:43 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/packages/HTML/QuickForm/Controller.php:203 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/packages/HTML/QuickForm/Page.php:103 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Controller.php:355 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Utils/Wrapper.php:98 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:295 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:69 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm/CRM/Core/Invoke.php:36 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm.php:121 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/components/com_civicrm/civicrm.php:40 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Dispatcher/LegacyComponentDispatcher.php:71 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Dispatcher/LegacyComponentDispatcher.php:73 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Component/ComponentHelper.php:361 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/AdministratorApplication.php:143 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/AdministratorApplication.php:186 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/libraries/src/Application/CMSApplication.php:293 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/includes/app.php:61 { …}
        /var/www/vhosts/xxx.xxx/httpdocs/administrator/index.php:32 { …}
  
  Running CiviCRM 5.68 , PHP 8.1 , Joomla 4.4.1
  
  **Tags:** contacts, duplicate-contacts
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-12-15. Score: 0
  
  According to this:
  Cannot add or update a child row: a foreign key constraint fails
  (`xxx.xxx`.`civicrm_activity`, CONSTRAINT `civicrm_activity_ibfk_3` 
  FOREIGN KEY (`parent_id`) REFERENCES `civicrm_activity_fudge` 
  (`id`) ON DELETE SET NULL)]
  
  you may want to check the civicrm_activity table structure, discover where the civicrm_activity_fudge has come from and edit or remove this reference.
  
  
  ---
  
  ### Answer by username_2 on 2023-12-15. Score: 0
  
  Run below query
  ALTER TABLE civicrm_activity_contact DROP FOREIGN KEY civicrm_activity_ibfk_3;
  
  ALTER TABLE civicrm_activity_contact 
  ADD CONSTRAINT `FK_civicrm_activity_contact_activity_id` FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE;
  
  Note: Take db backup before running query
  
  
  ---
  
  