Answers.Body: <p>The <code>civicrm_relationship</code> table is the canonical source of relationship data. It is a read/write table and will contain all the current records.</p>
  <p>The <code>civicrm_relationship_cache</code> is a read-only copy of the data. It will never be more current or correct, because it only ever copies data from the other table.</p>
  <p><code>civicrm_relationship_cache</code> should always contain exactly 2x number of rows as <code>civicrm_relationship</code> because it copies each relationship twice (once from the POV of each contact).</p>
  <p>It should always stay in-sync unless something goes wrong with your SQL triggers, in that case there is a system check which will automatically alert you and allow you to fix the problem with one click:</p>
  <p><a href="https://i.sstatic.net/N7umf.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/N7umf.png" alt="enter image description here" /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T16:14:34.493Z
Answers.Text: OK, I think that makes sense to me, and I don't see that warning so I'm probably good on that point.
  Do you have any suggestion about why I have relationships in the relationship table that do not appear on the relationship tabs of either contact?
Answers.UserDisplayName: zip
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T16:16:04.307Z
Answers.Text: Oh, and the "exactly 2x" isn't what I am seeing... does that indicate a problem?
Answers.UserDisplayName: zip
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T16:40:01.650Z
Answers.Text: The system check was added fairly recently so if you haven't upgraded since January you won't have it. Try just running the command to rebuild the table.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T17:22:58.653Z
Answers.Text: We're on 5.69.2 so I would assume fairly up to date. When I run cv api4 RelationshipCache.rebuild I get "Api RelationshipCache rebuild version 4 does not exist."
  I tried instead this command from another post (Might have been your answer then too!): cv ev 'CRM_Contact_BAO_RelationshipCache::rebuild();'
   That does look like it rebuild the cache. I'll explore whether that fixes/answers my issues. Thanks.
Answers.UserDisplayName: zip
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T15:55:13.867Z
Answers.LastActivityDate: 2024-02-25T14:56:38.007Z
Answers.LastEditDate: 2024-02-25T14:56:38.007Z
Answers.UserDisplayName: Coleman
Body: <p>I have a Drupal view that shows related contacts, and I noticed that it wasn't matching what I was seeing in the Civi relationships tabs on the relevant contacts. There were contacts displayed in the Drupal view with no relationships listed between those contacts in the Civi view.</p>
  <p>Some digging reveals (I think), that the Drupal view is using the relationship table and the Civi view is using the relationship_cache table, and some further digging makes me concerned something isn't flowing correctly.</p>
  <p>The specific example I'm looking at, there are relationships in the relationship table that do not exist in the relationship_cache table (and vice versa). I'm trying to work out any way of seeing these relationships in SearchKit or anywhere else in the interface, but the connections aren't appearing. They are in the database (and the Drupal view), but not accessible through Civi seemingly.</p>
  <p>Some data points:</p>
  <ul>
  <li>the relationship table has approximately 23,000 relationships</li>
  <li>the relationship_cache table has approximately 25,000 relationships</li>
  </ul>
  <p>I've read stuff about regenerating the cache table, but it really seems like the cache table is more accurate/current, or at least the non-cache table seems to have some out dated info.</p>
  <p>I'm not clear on how the two tables really relate nor how to navigate out of the problem.</p>
  
Tags: <relationships>
Title: How are the civicrm_relationship and the civicrm_relationship_cache tables supposed to interact?
ThreadText: # How are the civicrm_relationship and the civicrm_relationship_cache tables supposed to interact?
  
  (Asked by: username_0 on 2024-02-22)
  
  I have a Drupal view that shows related contacts, and I noticed that it wasn't matching what I was seeing in the Civi relationships tabs on the relevant contacts. There were contacts displayed in the Drupal view with no relationships listed between those contacts in the Civi view.
  
  Some digging reveals (I think), that the Drupal view is using the relationship table and the Civi view is using the relationship\_cache table, and some further digging makes me concerned something isn't flowing correctly.
  
  The specific example I'm looking at, there are relationships in the relationship table that do not exist in the relationship\_cache table (and vice versa). I'm trying to work out any way of seeing these relationships in SearchKit or anywhere else in the interface, but the connections aren't appearing. They are in the database (and the Drupal view), but not accessible through Civi seemingly.
  
  Some data points:
  
  * the relationship table has approximately 23,000 relationships
  * the relationship\_cache table has approximately 25,000 relationships
  
  I've read stuff about regenerating the cache table, but it really seems like the cache table is more accurate/current, or at least the non-cache table seems to have some out dated info.
  
  I'm not clear on how the two tables really relate nor how to navigate out of the problem.
  
  **Tags:** relationships
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-02-22. Score: 3 | Accepted answer
  
  The `civicrm_relationship` table is the canonical source of relationship data. It is a read/write table and will contain all the current records.
  
  The `civicrm_relationship_cache` is a read-only copy of the data. It will never be more current or correct, because it only ever copies data from the other table.
  
  `civicrm_relationship_cache` should always contain exactly 2x number of rows as `civicrm_relationship` because it copies each relationship twice (once from the POV of each contact).
  
  It should always stay in-sync unless something goes wrong with your SQL triggers, in that case there is a system check which will automatically alert you and allow you to fix the problem with one click:
  
  [![enter image description here](https://i.sstatic.net/N7umf.png)](https://i.sstatic.net/N7umf.png)
  
  > **Comments:**
  > **username_0**: OK, I think that makes sense to me, and I don't see that warning so I'm probably good on that point. Do you have any suggestion about why I have relationships in the relationship table that do not appear on the relationship tabs of either contact?  
  > **username_0**: Oh, and the "exactly 2x" isn't what I am seeing... does that indicate a problem?  
  > **username_1**: The system check was added fairly recently so if you haven't upgraded since January you won't have it. Try just running the command to rebuild the table.  
  > **username_0**: We're on 5.69.2 so I would assume fairly up to date. When I run cv api4 RelationshipCache.rebuild I get "Api RelationshipCache rebuild version 4 does not exist." I tried instead this command from another post (Might have been your answer then too!): cv ev 'CRM_Contact_BAO_RelationshipCache::rebuild();'  That does look like it rebuild the cache. I'll explore whether that fixes/answers my issues. Thanks.  
  
  ---
  
  