Answers.Body: <p>I can't reproduce the fail. Both api3 and api4 seem to work fine using the params as described. Perhaps there is something else involved on that site?</p>
  <p>e.g. both these work when there is already an existing relationship of the same type on the case with a different contact:</p>
  <p><code>cv ev --user=admin &quot;civicrm_api3('Relationship','create', [contact_id_a =&gt; 8, contact_id_b =&gt; 9, relationship_type_id =&gt; 14, is_active =&gt;1 , case_id =&gt; 1]);&quot;</code></p>
  <p><code>cv ev --user=admin &quot;\Civi\Api4\Relationship::create()-&gt;addValue('contact_id_a', 8)-&gt;addValue('contact_id_b', 10)-&gt;addValue('relationship_type_id', 14)-&gt;addValue('case_id', 1)-&gt;execute();&quot;</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-28T18:39:15.103Z
Answers.LastActivityDate: 2022-05-28T18:39:15.103Z
Answers.UserDisplayName: Demerit
Body: <p>I want to create some Relationships of type &quot;Case Coordinator&quot; via API Call in CiviCRM.</p>
  <p>When I create two relationships with the same contact_id_a, relationship_type_id, is_active and case_id values, but with a different contact_id_b value, I nevertheless get the error &quot;Duplicate Relationship thrown in .../public/sites/all/modules/civicrm/api/api.php on line 134&quot;.</p>
  <p>I have been checking the code in public/sites/all/modules/civicrm/CRM/Contact/BAO/Relationship.php and it seems to check the sameness of contact_id_a and contact_id_b correctly:</p>
  <pre><code>public static function checkDuplicateRelationship(&amp;$params, $id, $contactId = 0, $relationshipId = 0) {
   ...
      $queryString .=
        &quot; AND ( ( contact_id_a = &quot; . CRM_Utils_Type::escape($id, 'Integer') .
        &quot; AND contact_id_b = &quot; . CRM_Utils_Type::escape($contactId, 'Integer') .
        &quot; ) OR ( contact_id_a = &quot; . CRM_Utils_Type::escape($contactId, 'Integer') .
        &quot; AND contact_id_b = &quot; . CRM_Utils_Type::escape($id, 'Integer') . &quot; ) ) &quot;;
  ...
    }
  </code></pre>
  <p>Only when I add a distinct start_date to the API Create Relationship call, CiviCRM accepts the call and creates the relationship properly.</p>
  <p>This is ok as a workaround, but I wonder if anybody knows why the check on Contact A and Contact B in the code is not working here? As obviously two relationships are not a duplicate if they exist between a different pair of contacts.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-27T23:12:50.533Z
Comments.Text: have you confirmed if you hit the same issue with a non-civicase relationship? wondering if civicase throws in some extra hurdles
Comments.UserDisplayName: petednz - fuzion
Tags: <civicase><relationships>
Title: Duplicate Relationship error even though Contact B values are different
ThreadText: # Duplicate Relationship error even though Contact B values are different
  
  (Asked by: username_0 on 2022-05-27)
  
  I want to create some Relationships of type "Case Coordinator" via API Call in CiviCRM.
  When I create two relationships with the same contact_id_a, relationship_type_id, is_active and case_id values, but with a different contact_id_b value, I nevertheless get the error "Duplicate Relationship thrown in .../public/sites/all/modules/civicrm/api/api.php on line 134".
  I have been checking the code in public/sites/all/modules/civicrm/CRM/Contact/BAO/Relationship.php and it seems to check the sameness of contact_id_a and contact_id_b correctly:
  public static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) {
   ...
      $queryString .=
        " AND ( ( contact_id_a = " . CRM_Utils_Type::escape($id, 'Integer') .
        " AND contact_id_b = " . CRM_Utils_Type::escape($contactId, 'Integer') .
        " ) OR ( contact_id_a = " . CRM_Utils_Type::escape($contactId, 'Integer') .
        " AND contact_id_b = " . CRM_Utils_Type::escape($id, 'Integer') . " ) ) ";
  ...
    }
  
  Only when I add a distinct start_date to the API Create Relationship call, CiviCRM accepts the call and creates the relationship properly.
  This is ok as a workaround, but I wonder if anybody knows why the check on Contact A and Contact B in the code is not working here? As obviously two relationships are not a duplicate if they exist between a different pair of contacts.
  
  **Tags:** civicase, relationships
  
  > **Comments:**
  > **username_1**: have you confirmed if you hit the same issue with a non-civicase relationship? wondering if civicase throws in some extra hurdles  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-05-28. Score: 1
  
  I can't reproduce the fail. Both api3 and api4 seem to work fine using the params as described. Perhaps there is something else involved on that site?
  
  e.g. both these work when there is already an existing relationship of the same type on the case with a different contact:
  
  `cv ev --user=admin "civicrm_api3('Relationship','create', [contact_id_a => 8, contact_id_b => 9, relationship_type_id => 14, is_active =>1 , case_id => 1]);"`
  
  `cv ev --user=admin "\Civi\Api4\Relationship::create()->addValue('contact_id_a', 8)->addValue('contact_id_b', 10)->addValue('relationship_type_id', 14)->addValue('case_id', 1)->execute();"`
  
  
  ---
  
  