Answers.Body: <p>Groups are stored in <code>civicrm_group</code>.  Parent/child relationships are stored in <code>civicrm_group_nesting</code>.  However, based on the error you've posted, that's not the issue.  The issue is that the group was deleted, but there are still contacts who are a member of the group (which is stored in <code>civicrm_group_contact</code>).</p>
  
  <p>I'm not sure how you did that - as the error reports, there should be a "delete cascade" when you delete a group to remove the <code>civicrm_group_contact</code> records.  To fix this, make sure you have a backup, then:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/15501754/2832108">Temporarily disable foreign key constraints</a>.</li>
  <li>If your group id is 54, run <code>DELETE from civicrm_group_contact WHERE group_id = 54</code>.</li>
  <li>Re-enable your foreign key constraints.</li>
  </ul>
  
  <p>Hopefully that should clear this error!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-30T02:52:12.007Z
Answers.LastActivityDate: 2016-08-30T02:52:12.007Z
Answers.LastEditDate: 2017-05-23T12:38:59.763Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I need to manually delete a group that is corrupt. Already tried a few things. This corrupt group assigned itself as the parent to other groups and continues to add additional children groups every time 'managed groups' is open. I tried to delete the group via civicrm ui and directly in the db but then unable to edit any records or import records. Error message is below.</p>
  
  <p>I also manually removed parent child relationships directly in the database and then disabled the corrupt group in civicrm. This disabled all the group that were just removed as children.</p>
  
  <p>Also, cannot delete records from the group. And the number of records in this group changes every time 'managed group' is opened. Obviously corrupt and needs to be deleted. Any info on the data schema for groups and parent groups appreciated.  </p>
  
  <p>Here is the error when this corrupt group is deleted:</p>
  
  <blockquote>
    <p>$Fatal Error Details = Array (
        [callback] => Array
            (
                [0] => CRM_Core_Error
                [1] => handle
            )</p>
  
  <pre><code>[code] =&gt; -3
  [message] =&gt; DB Error: constraint violation
  [mode] =&gt; 16
  [debug_info] =&gt; REPLACE INTO civicrm_group_contact ( group_id, contact_id, status ) VALUES ( 58, 39435, 'Added' ) [nativecode=1452 **
  </code></pre>
    
    <p>Cannot add or update a child row: a foreign key constraint fails
    (<code>xxx_civicrm</code>.<code>civicrm_group_contact</code>, CONSTRAINT
    <code>FK_civicrm_group_contact_group_id</code> FOREIGN KEY (<code>group_id</code>)
    REFERENCES <code>civicrm_group</code> (<code>id</code>) ON DELETE CASCADE)]</p>
  </blockquote>
  
Tags: <database><groups><data-model>
Title: What is the database schema for groups and parent groups?
ThreadText: # What is the database schema for groups and parent groups?
  
  (Asked by: username_0 on 2016-08-29)
  
  I need to manually delete a group that is corrupt. Already tried a few things. This corrupt group assigned itself as the parent to other groups and continues to add additional children groups every time 'managed groups' is open. I tried to delete the group via civicrm ui and directly in the db but then unable to edit any records or import records. Error message is below.
  
  I also manually removed parent child relationships directly in the database and then disabled the corrupt group in civicrm. This disabled all the group that were just removed as children.
  
  Also, cannot delete records from the group. And the number of records in this group changes every time 'managed group' is opened. Obviously corrupt and needs to be deleted. Any info on the data schema for groups and parent groups appreciated.
  
  Here is the error when this corrupt group is deleted:
  
  > $Fatal Error Details = Array (
  > [callback] => Array
  > (
  > [0] => CRM\_Core\_Error
  > [1] => handle
  > )
  >
  > ```
  > [code] => -3
  > [message] => DB Error: constraint violation
  > [mode] => 16
  > [debug_info] => REPLACE INTO civicrm_group_contact ( group_id, contact_id, status ) VALUES ( 58, 39435, 'Added' ) [nativecode=1452 **
  >
  > ```
  >
  > Cannot add or update a child row: a foreign key constraint fails
  > (`xxx_civicrm`.`civicrm_group_contact`, CONSTRAINT
  > `FK_civicrm_group_contact_group_id` FOREIGN KEY (`group_id`)
  > REFERENCES `civicrm_group` (`id`) ON DELETE CASCADE)]
  
  **Tags:** database, groups, data-model
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-30. Score: 1
  
  Groups are stored in `civicrm_group`. Parent/child relationships are stored in `civicrm_group_nesting`. However, based on the error you've posted, that's not the issue. The issue is that the group was deleted, but there are still contacts who are a member of the group (which is stored in `civicrm_group_contact`).
  
  I'm not sure how you did that - as the error reports, there should be a "delete cascade" when you delete a group to remove the `civicrm_group_contact` records. To fix this, make sure you have a backup, then:
  
  * [Temporarily disable foreign key constraints](https://stackoverflow.com/a/15501754/2832108).
  * If your group id is 54, run `DELETE from civicrm_group_contact WHERE group_id = 54`.
  * Re-enable your foreign key constraints.
  
  Hopefully that should clear this error!
  
  
  ---
  
  