<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Updating a table in arcpy without affecting its relationship classes in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/updating-a-table-in-arcpy-without-affecting-its/m-p/1338915#M6795</link>
    <description>&lt;P&gt;I am experiencing trouble in my script and I can't pin down what the issue is.&amp;nbsp; The goal is to update a table from an excel file.&amp;nbsp; The existing table has Relationship Classes relating it to each feature class.&amp;nbsp; I have a function to check which feature classes need Relationship Classes (if any, in this example all feature classes already have a Relationship Class).&amp;nbsp; If there is a relationship class name containing a specified keyword (in this case 'wrqst'), then a new relationship does not need to be created.&amp;nbsp; Otherwise, a different function will create the necessary relationships.&amp;nbsp; Here is the snippet giving me issues:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def checkRelationshipClasses(dataSource): # NOTE: dataSource should be a sting like 'wrqst' or 'wrcpt'. Also, Relationship Class Names should always include either wrqst or wrcpt in the name!
    results = {} # Dictionary to hold list of feature classes and list of boolean values
    bools = [] # List of boolean values indicating whether relate exists for feature class
    arcpy.env.workspace = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    featureClasses = arcpy.ListFeatureClasses() # List of feature classes
    results['featureClasses'] = featureClasses # add list of feature classes to results dictionary
    for fc in featureClasses:
        print(f"fc: {fc}")
        desc = arcpy.Describe(fc)
        relationshipClasses = desc.relationshipClassNames
        print(f"relationship classes: {relationshipClasses}")
        relationshipStr = (' '.join(relationshipClasses)).lower() # Get all relationship class names as lowercase string
        if dataSource in relationshipStr:
            print(f"data src -{dataSource}- is in string -{relationshipStr}-")
            bools.append(True)  # if 'dataSource' is in the string, the value is True, indicating there is a 'dataSource' relationship class
        else:
            print(f"data src -{dataSource}- is NOT in string -{relationshipStr}-")
            bools.append(False)
    results['bools'] = bools # else the value is False, indicating there is no 'dataSource' relationship class
    print(f"CheckRelationshipClasses results: {results}")

    classesThatNeedRelates = [] # List of classes where bool is false
    for i in range(len(results['featureClasses'])):
        if results['bools'][i] == True:
            pass
            #print(f"{results['featureClasses'][i]} already has a {dataSource} join")
        else:
            classesThatNeedRelates.append(results['featureClasses'][i]) # Add this feature class to list of classesThatNeedRelates
    print(f"Classes that need relates: {classesThatNeedRelates}")
    return classesThatNeedRelates&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I set some variables, update my&amp;nbsp; table from the excel file, call my Relationship Class checking function, and call another function that creates relationship classes if needed (which is not needed in this case).&amp;nbsp; If I run this code with lines 9 and 10 commented as seen below, it works as I would expect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if __name__ == "__main__":

    # variables
    originPath = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    workspace = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    

    # Update table from excel file --These Lines Break The Script--
    #arcpy.env.overwriteOutput = True # To avoid error 000725 (output table already exists)
    #arcpy.conversion.ExcelToTable(r"C:\\Users\\twhammond\\Documents\\tommy_misc\\XL\\temp.xlsx",r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\WRQSTTable") # Turn temp excel file into arcGIS table called WRQSTTable


    # Check if relates need to be created
    classesThatNeedWRQST = checkRelationshipClasses('wrqst')
    for i in range(len(classesThatNeedWRQST)):
        classesThatNeedWRQST[i] = originPath + classesThatNeedWRQST[i]


    if len(classesThatNeedWRQST) &amp;gt; 0:
        relateWRQSTData(
            classesThatNeedWRQST, 
            originPrimaryKey='PNUMDATA', 
            originForeignKey='PARCEL'
            )
    else:
        print('no new WRQST relationship needed')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the output, looks good to me.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="twhammond_2-1697488663319.png" style="width: 788px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83181iAE5DE3B898183CA2/image-dimensions/788x133?v=v2" width="788" height="133" role="button" title="twhammond_2-1697488663319.png" alt="twhammond_2-1697488663319.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;However, if I uncomment those lines to update the table, this is what I get:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="twhammond_3-1697577172065.png" style="width: 782px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83314i1D8B8A3EE285F52E/image-dimensions/782x112?v=v2" width="782" height="112" role="button" title="twhammond_3-1697577172065.png" alt="twhammond_3-1697577172065.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;It seems to me that overwriting the table using ExcelToTable breaks the relationship class, so I guess the real question is, can I somehow populate the related table with all new entries in such a way that preserves the relationship class?&amp;nbsp; Or will I need to recreate every relationship class every time the table is updated?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Oct 2023 14:41:39 GMT</pubDate>
    <dc:creator>twhammond</dc:creator>
    <dc:date>2023-10-23T14:41:39Z</dc:date>
    <item>
      <title>Updating a table in arcpy without affecting its relationship classes</title>
      <link>https://community.esri.com/t5/developers-questions/updating-a-table-in-arcpy-without-affecting-its/m-p/1338915#M6795</link>
      <description>&lt;P&gt;I am experiencing trouble in my script and I can't pin down what the issue is.&amp;nbsp; The goal is to update a table from an excel file.&amp;nbsp; The existing table has Relationship Classes relating it to each feature class.&amp;nbsp; I have a function to check which feature classes need Relationship Classes (if any, in this example all feature classes already have a Relationship Class).&amp;nbsp; If there is a relationship class name containing a specified keyword (in this case 'wrqst'), then a new relationship does not need to be created.&amp;nbsp; Otherwise, a different function will create the necessary relationships.&amp;nbsp; Here is the snippet giving me issues:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def checkRelationshipClasses(dataSource): # NOTE: dataSource should be a sting like 'wrqst' or 'wrcpt'. Also, Relationship Class Names should always include either wrqst or wrcpt in the name!
    results = {} # Dictionary to hold list of feature classes and list of boolean values
    bools = [] # List of boolean values indicating whether relate exists for feature class
    arcpy.env.workspace = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    featureClasses = arcpy.ListFeatureClasses() # List of feature classes
    results['featureClasses'] = featureClasses # add list of feature classes to results dictionary
    for fc in featureClasses:
        print(f"fc: {fc}")
        desc = arcpy.Describe(fc)
        relationshipClasses = desc.relationshipClassNames
        print(f"relationship classes: {relationshipClasses}")
        relationshipStr = (' '.join(relationshipClasses)).lower() # Get all relationship class names as lowercase string
        if dataSource in relationshipStr:
            print(f"data src -{dataSource}- is in string -{relationshipStr}-")
            bools.append(True)  # if 'dataSource' is in the string, the value is True, indicating there is a 'dataSource' relationship class
        else:
            print(f"data src -{dataSource}- is NOT in string -{relationshipStr}-")
            bools.append(False)
    results['bools'] = bools # else the value is False, indicating there is no 'dataSource' relationship class
    print(f"CheckRelationshipClasses results: {results}")

    classesThatNeedRelates = [] # List of classes where bool is false
    for i in range(len(results['featureClasses'])):
        if results['bools'][i] == True:
            pass
            #print(f"{results['featureClasses'][i]} already has a {dataSource} join")
        else:
            classesThatNeedRelates.append(results['featureClasses'][i]) # Add this feature class to list of classesThatNeedRelates
    print(f"Classes that need relates: {classesThatNeedRelates}")
    return classesThatNeedRelates&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I set some variables, update my&amp;nbsp; table from the excel file, call my Relationship Class checking function, and call another function that creates relationship classes if needed (which is not needed in this case).&amp;nbsp; If I run this code with lines 9 and 10 commented as seen below, it works as I would expect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if __name__ == "__main__":

    # variables
    originPath = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    workspace = r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\"
    

    # Update table from excel file --These Lines Break The Script--
    #arcpy.env.overwriteOutput = True # To avoid error 000725 (output table already exists)
    #arcpy.conversion.ExcelToTable(r"C:\\Users\\twhammond\\Documents\\tommy_misc\\XL\\temp.xlsx",r"C:\Users\twhammond\Documents\tommy_misc\dataProject\testFiles\mygdb.gdb\\WRQSTTable") # Turn temp excel file into arcGIS table called WRQSTTable


    # Check if relates need to be created
    classesThatNeedWRQST = checkRelationshipClasses('wrqst')
    for i in range(len(classesThatNeedWRQST)):
        classesThatNeedWRQST[i] = originPath + classesThatNeedWRQST[i]


    if len(classesThatNeedWRQST) &amp;gt; 0:
        relateWRQSTData(
            classesThatNeedWRQST, 
            originPrimaryKey='PNUMDATA', 
            originForeignKey='PARCEL'
            )
    else:
        print('no new WRQST relationship needed')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the output, looks good to me.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="twhammond_2-1697488663319.png" style="width: 788px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83181iAE5DE3B898183CA2/image-dimensions/788x133?v=v2" width="788" height="133" role="button" title="twhammond_2-1697488663319.png" alt="twhammond_2-1697488663319.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;However, if I uncomment those lines to update the table, this is what I get:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="twhammond_3-1697577172065.png" style="width: 782px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83314i1D8B8A3EE285F52E/image-dimensions/782x112?v=v2" width="782" height="112" role="button" title="twhammond_3-1697577172065.png" alt="twhammond_3-1697577172065.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;It seems to me that overwriting the table using ExcelToTable breaks the relationship class, so I guess the real question is, can I somehow populate the related table with all new entries in such a way that preserves the relationship class?&amp;nbsp; Or will I need to recreate every relationship class every time the table is updated?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2023 14:41:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/updating-a-table-in-arcpy-without-affecting-its/m-p/1338915#M6795</guid>
      <dc:creator>twhammond</dc:creator>
      <dc:date>2023-10-23T14:41:39Z</dc:date>
    </item>
  </channel>
</rss>

