Answers.Body: <p>I've tried this with some fake information and it seems to work in practice -- is there anything dangerous about deleting directly in the database all memberships that have an <code>owner_membership_id</code> that is not NULL?</p>
  
  <p><strong>Steps:</strong></p>
  
  <ol>
  <li>Delete non-primary memberships in the database (<code>owner_membership_id</code> is not NULL)*</li>
  <li>Export all memberships</li>
  <li>Import those same memberships without changing any information, making sure to select "Update" on the import screen and matching up the membership ID with the correct field.</li>
  </ol>
  
  <hr>
  
  <p><em>Example SQL for #1 above:</em></p>
  
  <pre><code>DELETE 
  FROM civicrm_membership
  WHERE owner_membership_id IS NOT NULL;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-28T07:52:26.753Z
Answers.Text: I would also recommend checking following tables:
  - civicrm_membership_payment
  - civicrm_membership_log
  - If you have used price sets then civicrm_line_item
Answers.UserDisplayName: Kurund Jalmi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T18:07:53.397Z
Answers.LastActivityDate: 2015-10-28T03:31:08.260Z
Answers.LastEditDate: 2015-10-28T03:31:08.260Z
Answers.UserDisplayName: Laryn
Answers.Body: <p>I ran into a situation where the membership-type relationships were missing a relationship type, so I had to re-generate the related memberships.</p>
  
  <p>Instead of export/import, I used the CiviCRM API:</p>
  
  <pre><code>$result = civicrm_api3('Membership', 'get', [
    'membership_type_id' =&gt; 1234, // optional
    'option.limit' =&gt; 0,
  ]);
  
  foreach ($result['values'] as $key =&gt; $val) {
    civicrm_api3('Membership', 'create', [
      'id' =&gt; $val['id'],
    ]);
  }
  </code></pre>
  
  <p>The Membership BAO calls <code>createRelatedMemberships</code>.</p>
  
  <p>update: however, I had a small issue with the Membership dashboard not reflecting the numbers correctly, and had to run this query:</p>
  
  <pre><code>UPDATE civicrm_membership m
    LEFT OUTER JOIN civicrm_membership parent ON (parent.id = m.owner_membership_id)
  SET m.join_date = parent.join_date, m.end_date = parent.end_date, m.start_date = parent.start_date
  WHERE m.membership_type_id = 1234
    AND m.start_date is null;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T19:40:00.527Z
Answers.LastActivityDate: 2017-02-16T20:48:55.670Z
Answers.LastEditDate: 2017-02-16T20:48:55.670Z
Answers.UserDisplayName: bgm
Body: <p>Is there a way to do a one-time purge and regenerate of all memberships that are shared by a relationship? (I would be okay if it required doing some SQL directly in the database as long as it didn't conflict with anything else).</p>
  
  <p>The issue is that some relationships were deleted directly in the database and I want to make sure all the memberships are up-to-date.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-08-14T14:08:57.100Z
Comments.Text: Would also like to know how this can be done as I have changed the Relationship direction in our system as it was incorrect, however I need to update all memberships for this to take effect. @Laryn did you manage to get anywhere with this? Thanks.
Comments.UserDisplayName: NathanB
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-08-14T19:30:33.627Z
Comments.Text: @NathanB Not yet, but I'm still working it through. If no one has a suggestion off the top I suspect I'll figure something out within the week as I have time to devote to it and I'll post back here.
Comments.UserDisplayName: Laryn
Tags: <civimember><relationships>
Title: How can I regenerate memberships shared by relationship?
ThreadText: # How can I regenerate memberships shared by relationship?
  
  (Asked by: username_0 on 2015-08-13)
  
  Is there a way to do a one-time purge and regenerate of all memberships that are shared by a relationship? (I would be okay if it required doing some SQL directly in the database as long as it didn't conflict with anything else).
  
  The issue is that some relationships were deleted directly in the database and I want to make sure all the memberships are up-to-date.
  
  **Tags:** civimember, relationships
  
  > **Comments:**
  > **username_1**: Would also like to know how this can be done as I have changed the Relationship direction in our system as it was incorrect, however I need to update all memberships for this to take effect. @username_0 did you manage to get anywhere with this? Thanks.  
  > **username_0**: @username_1 Not yet, but I'm still working it through. If no one has a suggestion off the top I suspect I'll figure something out within the week as I have time to devote to it and I'll post back here.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2017-02-16. Score: 3 | Accepted answer
  
  I ran into a situation where the membership-type relationships were missing a relationship type, so I had to re-generate the related memberships.
  
  Instead of export/import, I used the CiviCRM API:
  
  ```
  $result = civicrm_api3('Membership', 'get', [
    'membership_type_id' => 1234, // optional
    'option.limit' => 0,
  ]);
  
  foreach ($result['values'] as $key => $val) {
    civicrm_api3('Membership', 'create', [
      'id' => $val['id'],
    ]);
  }
  
  ```
  
  The Membership BAO calls `createRelatedMemberships`.
  
  update: however, I had a small issue with the Membership dashboard not reflecting the numbers correctly, and had to run this query:
  
  ```
  UPDATE civicrm_membership m
    LEFT OUTER JOIN civicrm_membership parent ON (parent.id = m.owner_membership_id)
  SET m.join_date = parent.join_date, m.end_date = parent.end_date, m.start_date = parent.start_date
  WHERE m.membership_type_id = 1234
    AND m.start_date is null;
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2015-10-23. Score: 2
  
  I've tried this with some fake information and it seems to work in practice -- is there anything dangerous about deleting directly in the database all memberships that have an `owner_membership_id` that is not NULL?
  
  **Steps:**
  
  1. Delete non-primary memberships in the database (`owner_membership_id` is not NULL)\*
  2. Export all memberships
  3. Import those same memberships without changing any information, making sure to select "Update" on the import screen and matching up the membership ID with the correct field.
  
  ---
  
  *Example SQL for #1 above:*
  
  ```
  DELETE 
  FROM civicrm_membership
  WHERE owner_membership_id IS NOT NULL;
  
  ```
  
  > **Comments:**
  > **username_2**: I would also recommend checking following tables: - civicrm_membership_payment - civicrm_membership_log - If you have used price sets then civicrm_line_item  
  
  ---
  
  