Write to Feature Class and Related Table

181
2
Jump to solution
04-22-2020 12:33 PM
jaykapalczynski
Frequent Contributor

I have a Feature Class and a Table and a Relationship Class relating them via GlobalID

I want to write to the Feature Class (geometry and a couple attributes)

I also want to write to the related table (few attributes)

Are there any simple examples of how to write to them via arcPy?

I can write to the Feature Class like the below BUT how do I write to the related Table at the same time?

    workspace = r"E:/ArcGISProjects/CountySelection/cccc.sde"
    edit = arcpy.da.Editor(workspace)
    # Edit session is started without an undo/redo stack for versioned data
    #  (for second argument, use False for unversioned data)
    edit.startEditing(False, False)
    print "edit started"
    # Create update cursor
    with arcpy.da.InsertCursor(inputFeatureclass, ['SHAPE@', 'ADDRESSgeocode', 'DISTANCEparameter', 'UNIQUEIDparameter', 'COUNTYLIST', 'ID']) as cursor:
        # Start an edit operation
        edit.startOperation()
        # Perform edits
        ## CREATE THE NEW MULTI PART POLYGON IN THE FC
        cursor.insertRow([geometries[0], searchAddress, searchDistance, searchid, txt_list, validGuid])
        ## Delete cursor object
        del cursor

    # Stop the edit operation.
    edit.stopOperation()
    # Stop the edit session and save the changes
    edit.stopEditing(True)
    print "edit ended"
0 Kudos
1 Solution

Accepted Solutions
jaykapalczynski
Frequent Contributor

This seems to work...I write to the FC and then to the table

BUT for some reason this python script works when ran from ArcCatalog BUT does not work after published to a GP Service.

If I remove the relationship class it works great....add the relationship class back and it fails from JavaScript

ugggggggg

Do I have to write to the FC and add a unique ID and then write to the related table and add that same unique ID (thats what I am doing below)

or

Can I write to the FC and have it automatically add a record in the related table with some unique ID

Just sort of confused by the whole process and on top of that the GP Service works in Catalog although does NOT when I publish it with the Relationship Class....

I delete the Relationship Class and it works fine????/  Puzzled here

with arcpy.da.InsertCursor(input_Featureclass, ['SHAPE@', 'ADDRESSgeocode', 'DISTANCEparameter', 'UNIQUEIDparameter', 'COUNTYLIST', 'NWTLID']) as cursor:

    edit.startOperation()
    # Perform edits
    cursor.insertRow([geometries[0], validGuid])
    with arcpy.da.InsertCursor(input_Table, ['ID', 'FieldID']) as cursor2:
       cursor2.insertRow([validGuid, 1520])
       del cursor2
    ## Delete cursor object
    del cursor‍‍‍‍‍‍‍‍

# SNIP‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
2 Replies
jaykapalczynski
Frequent Contributor

Might be drawing at straws here:

    input_Featureclass = r"E:/ArcGISProjects/County/cccc.sde/MultiPartPoly"
    input_Table = r"E:/ArcGISProjects/County/cccc.sde/AttributeTable"
    relatedTables = arcpy.ListTables(input_Table) 
  

    # Create update cursor
    with arcpy.da.InsertCursor(input_Featureclass, ['SHAPE@', 'ID']) as cursor:
        # Start an edit operation
        edit.startOperation()
        # Perform edits
        ## CREATE THE NEW MULTI PART POLYGON IN THE FC
        for cursors in cursor:
            cursor.insertRow([geometries[0], validGuid])
            for relatedTable in relatedTables:
                with arcpy.da.InsertCursor(input_Table, ['ID', 'somefieldID']) as cursor2:
                    cursor2.insertRow([validGuid, 1520])
                    del cursor2
        ## Delete cursor object
        del cursor
‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
jaykapalczynski
Frequent Contributor

This seems to work...I write to the FC and then to the table

BUT for some reason this python script works when ran from ArcCatalog BUT does not work after published to a GP Service.

If I remove the relationship class it works great....add the relationship class back and it fails from JavaScript

ugggggggg

Do I have to write to the FC and add a unique ID and then write to the related table and add that same unique ID (thats what I am doing below)

or

Can I write to the FC and have it automatically add a record in the related table with some unique ID

Just sort of confused by the whole process and on top of that the GP Service works in Catalog although does NOT when I publish it with the Relationship Class....

I delete the Relationship Class and it works fine????/  Puzzled here

with arcpy.da.InsertCursor(input_Featureclass, ['SHAPE@', 'ADDRESSgeocode', 'DISTANCEparameter', 'UNIQUEIDparameter', 'COUNTYLIST', 'NWTLID']) as cursor:

    edit.startOperation()
    # Perform edits
    cursor.insertRow([geometries[0], validGuid])
    with arcpy.da.InsertCursor(input_Table, ['ID', 'FieldID']) as cursor2:
       cursor2.insertRow([validGuid, 1520])
       del cursor2
    ## Delete cursor object
    del cursor‍‍‍‍‍‍‍‍

# SNIP‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos