Answers.Body: <p>We have had a similar problem - <a href="https://gist.githubusercontent.com/deepak-srivastava/e3a440023621cd4ecbde3904f9b27d2c/raw/7d2e854aa4ad7a8f00d87581dca729623d5ba687/bto-membership-by-relationship-error.log" rel="nofollow noreferrer">https://gist.githubusercontent.com/deepak-srivastava/e3a440023621cd4ecbde3904f9b27d2c/raw/7d2e854aa4ad7a8f00d87581dca729623d5ba687/bto-membership-by-relationship-error.log</a></p>
  
  <p>Seems like method createRelatedMemberships(&amp;$params, &amp;$dao, $reset = FALSE) in loop sometimes resets $params.
  The while loop at the end of the function seems to be the cause. Don't see why anything from queue should reset it. </p>
  
  <p>Have developed a patch - <a href="https://github.com/deepak-srivastava/civicrm-core/commit/96fba9b2533b45d9baaef8b0f7ea43bc85ae0ee9" rel="nofollow noreferrer">https://github.com/deepak-srivastava/civicrm-core/commit/96fba9b2533b45d9baaef8b0f7ea43bc85ae0ee9</a></p>
  
  <p>Seems to be working so far. We 'll test it a bit more, and if ok submit a PR to core. 
  Meanwhile would you be able to confirm if the patch works for you?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-31T12:48:29.867Z
Answers.Text: Hi Deepak, thanks for this and sorry for the slow response. The problem was occurring on one client's site two years ago and I wasn't able to replicate it (without the patch) when I tried after reading your answer. We have asked the client whether they have encountered the problem recently but so far have had no response. I'll update here if we hear anything.
Answers.UserDisplayName: davejenx
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-27T15:02:12.660Z
Answers.LastActivityDate: 2017-07-27T15:02:12.660Z
Answers.UserDisplayName: Deepak Srivastava
Body: <p>I have borrowed the title from this unanswered forum post by <a href="https://civicrm.stackexchange.com/users/26/laryn">Laryn</a>: <a href="https://forum.civicrm.org/index.php?topic=33192.0" rel="nofollow">DeDupe errors when Individuals have Membership (by relationship)</a>.</p>
  
  <p>Just encountered the same error on a Civi 4.6.9 / Drupal 7 site. Laryn, did you get anywhere with this? Or can anyone shed any light? It appears that CRM_Member_BAO_Membership::createRelatedMemberships(), via some intermediate function calls, tried to insert a civicrm_membership record without specifying membership_type_id: our error &amp; backtrace is very similar to Laryn's:</p>
  
  <p>[debug_info] => INSERT INTO civicrm_membership (contact_id , status_id , owner_membership_id ) VALUES ( 15835 ,  2 ,  21422 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (<code>my_civicrm</code>.<code>civicrm_membership</code>, CONSTRAINT <code>FK_civicrm_membership_membership_type_id</code> FOREIGN KEY (<code>membership_type_id</code>) REFERENCES <code>civicrm_membership_type</code> (<code>id</code>) ON DELETE CASCA)]</p>
  
  <p>Excerpt from backtrace:</p>
  
  <pre><code>#10 .../civicrm/packages/DB/DataObject.php(1060): DB_DataObject-&gt;_query("INSERT INTO civicrm_membership (contact_id , status_id , owner_membership_id ...")
  #11 .../civicrm/CRM/Core/DAO.php(450): DB_DataObject-&gt;insert()
  #12 .../civicrm/CRM/Member/BAO/Membership.php(102): CRM_Core_DAO-&gt;save()
  #13 .../civicrm/CRM/Member/BAO/Membership.php(310): CRM_Member_BAO_Membership::add((Array:6), (Array:0))
  #14 .../civicrm/CRM/Member/BAO/Membership.php(1941): CRM_Member_BAO_Membership::create((Array:6), (Array:0))
  #15 .../civicrm/CRM/Dedupe/Merger.php(1655): CRM_Member_BAO_Membership::createRelatedMemberships((Array:4), Object(CRM_Member_DAO_Membership))
  #16 .../civicrm/CRM/Dedupe/Merger.php(512): CRM_Dedupe_Merger::addMembershipToRealtedContacts(6428)
  #17 .../civicrm/CRM/Dedupe/Merger.php(1349): CRM_Dedupe_Merger::moveContactBelongings("6428", "36044")
  #18 .../civicrm/CRM/Dedupe/Merger.php(660): CRM_Dedupe_Merger::moveAllBelongings("6428", "36044", (Array:6))
  #19 .../civicrm/CRM/Dedupe/Merger.php(596): CRM_Dedupe_Merger::merge((Array:75), (Array:3), "safe", TRUE, TRUE)
  #20 .../civicrm/CRM/Contact/Page/DedupeFind.php(97): CRM_Dedupe_Merger::batchMerge("11", "48", "safe", TRUE, TRUE)
  #21 .../civicrm/CRM/Core/Invoke.php(312): CRM_Contact_Page_DedupeFind-&gt;run((Array:3), NULL)
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-02T17:33:02.357Z
Comments.Text: Sorry, I didn't get much traction in the forum post, obviously, and since it didn't seem to happen in the demo site when I tried to test there, I assumed it was something in our database. We muddled through these and have recently ended up regenerating all shared memberships to make sure those were up to speed after some fiddling in the database: http://civicrm.stackexchange.com/questions/4703/how-can-i-regenerate-memberships-shared-by-relationship
Comments.UserDisplayName: Laryn
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-03T10:33:19.863Z
Comments.Text: Hi Laryn, thanks for the response. Did regenerating the inherited memberships resolve the problem?
Comments.UserDisplayName: davejenx
Tags: <civimember><duplicate-contacts>
Title: DeDupe errors when Individuals have Membership (by relationship)
ThreadText: # DeDupe errors when Individuals have Membership (by relationship)
  
  (Asked by: username_0 on 2015-10-30)
  
  I have borrowed the title from this unanswered forum post by username_1: DeDupe errors when Individuals have Membership (by relationship).
  Just encountered the same error on a Civi 4.6.9 / Drupal 7 site. username_1, did you get anywhere with this? Or can anyone shed any light? It appears that CRM_Member_BAO_Membership::createRelatedMemberships(), via some intermediate function calls, tried to insert a civicrm_membership record without specifying membership_type_id: our error & backtrace is very similar to username_1's:
  [debug_info] => INSERT INTO civicrm_membership (contact_id , status_id , owner_membership_id ) VALUES ( 15835 ,  2 ,  21422 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (my_civicrm.civicrm_membership, CONSTRAINT FK_civicrm_membership_membership_type_id FOREIGN KEY (membership_type_id) REFERENCES civicrm_membership_type (id) ON DELETE CASCA)]
  Excerpt from backtrace:
  #10 .../civicrm/packages/DB/DataObject.php(1060): DB_DataObject->_query("INSERT INTO civicrm_membership (contact_id , status_id , owner_membership_id ...")
  #11 .../civicrm/CRM/Core/DAO.php(450): DB_DataObject->insert()
  #12 .../civicrm/CRM/Member/BAO/Membership.php(102): CRM_Core_DAO->save()
  #13 .../civicrm/CRM/Member/BAO/Membership.php(310): CRM_Member_BAO_Membership::add((Array:6), (Array:0))
  #14 .../civicrm/CRM/Member/BAO/Membership.php(1941): CRM_Member_BAO_Membership::create((Array:6), (Array:0))
  #15 .../civicrm/CRM/Dedupe/Merger.php(1655): CRM_Member_BAO_Membership::createRelatedMemberships((Array:4), Object(CRM_Member_DAO_Membership))
  #16 .../civicrm/CRM/Dedupe/Merger.php(512): CRM_Dedupe_Merger::addMembershipToRealtedContacts(6428)
  #17 .../civicrm/CRM/Dedupe/Merger.php(1349): CRM_Dedupe_Merger::moveContactBelongings("6428", "36044")
  #18 .../civicrm/CRM/Dedupe/Merger.php(660): CRM_Dedupe_Merger::moveAllBelongings("6428", "36044", (Array:6))
  #19 .../civicrm/CRM/Dedupe/Merger.php(596): CRM_Dedupe_Merger::merge((Array:75), (Array:3), "safe", TRUE, TRUE)
  #20 .../civicrm/CRM/Contact/Page/DedupeFind.php(97): CRM_Dedupe_Merger::batchMerge("11", "48", "safe", TRUE, TRUE)
  #21 .../civicrm/CRM/Core/Invoke.php(312): CRM_Contact_Page_DedupeFind->run((Array:3), NULL)
  
  **Tags:** civimember, duplicate-contacts
  
  > **Comments:**
  > **username_1**: Sorry, I didn't get much traction in the forum post, obviously, and since it didn't seem to happen in the demo site when I tried to test there, I assumed it was something in our database. We muddled through these and have recently ended up regenerating all shared memberships to make sure those were up to speed after some fiddling in the database: http://civicrm.stackexchange.com/questions/4703/how-can-i-regenerate-memberships-shared-by-relationship  
  > **username_0**: Hi username_1, thanks for the response. Did regenerating the inherited memberships resolve the problem?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-07-27. Score: 2
  
  We have had a similar problem - <https://gist.githubusercontent.com/deepak-srivastava/e3a440023621cd4ecbde3904f9b27d2c/raw/7d2e854aa4ad7a8f00d87581dca729623d5ba687/bto-membership-by-relationship-error.log>
  
  Seems like method createRelatedMemberships(&$params, &$dao, $reset = FALSE) in loop sometimes resets $params.
  The while loop at the end of the function seems to be the cause. Don't see why anything from queue should reset it.
  
  Have developed a patch - <https://github.com/deepak-srivastava/civicrm-core/commit/96fba9b2533b45d9baaef8b0f7ea43bc85ae0ee9>
  
  Seems to be working so far. We 'll test it a bit more, and if ok submit a PR to core.
  Meanwhile would you be able to confirm if the patch works for you?
  
  > **Comments:**
  > **username_0**: Hi Deepak, thanks for this and sorry for the slow response. The problem was occurring on one client's site two years ago and I wasn't able to replicate it (without the patch) when I tried after reading your answer. We have asked the client whether they have encountered the problem recently but so far have had no response. I'll update here if we hear anything.  
  
  ---
  
  