InsertCursor return GlobalID

915
1
Jump to solution
02-06-2018 06:48 PM
CallumSmith2
Occasional Contributor

Hi

Is there any way using a arcpy.da.insertCursor for it to return a GlobalIID rather than the OBJECTID? 

My script is adding data to a number of feature classes and tables that are all tied together with relationship classes using GlobalID's and keys. The purpose of my script is to normalise some data in a table that has been captured with Survey123.

Currently my workflow is as follows

1. Create a edit session

2. Create InsertCursors for all my tables.

3. Loop through my input adding records to the necessary tables. After Each insert I get the OBJECTID and then create a SearchCursor on the same table with the OBJECTID as the where. I then extract the GlobalID from the record in the search cursor.

4. delete Insert Cursors

5. Stop editing and Commit.

This approach works for some of my tables but not for others? My guess there is some kind if issue / bug when triggering a search cursor on a table that is in edit mode on a record that has just been added and not committed?

Is there a better approach for this to get a GlobalID from a record that has just been inserted?  I don't want to have to stop editing after each insert to create a SearchCursor get the GlobalID as this will potentially be slow.

cheers

Callum

0 Kudos
1 Solution

Accepted Solutions
CallumSmith2
Occasional Contributor

I have figured out what the issue was here and why I could get the GlobalID for some featureclasses/tables and not others. Consider the following code snippet.

fc = 'featureclass'
insertFields = ['field1', 'field2']
with arcpy.da.InsertCursor(fc, insertFields) as insertCursor:
   row = ['Blah', 'Blah']
   objectid = insertCursor.insertRow(row)

   searchFields = ['OBJECTID', 'GLOBALID']
   whereClause = 'OBJECTID = {0}'.format(objectid)
   with arcpy.da.SearchCursor(fc, searchFields, whereClause) as searchCursor:
       for row in searchCursor:
           globalid = row[searchFields.index('GLOBALID')]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For a featureclass I am trying to extract the GlobalID from the record that has just been inserted. This works for some of my featureclasses/tables but not others. After some testing (and hair pulling!) I have identified for the above code to work and return the globalid the featureclass has to participate in a composite relationship class with the globalid as the primary key. If the featureclass does not participate in a composite relationship class then the search cursor will not be able to find the previously inserted record.

If anyone has a better method of getting the globalid for a record that has just been inserted I would be interested in hearing from you.

cheers

Callum

View solution in original post

1 Reply
CallumSmith2
Occasional Contributor

I have figured out what the issue was here and why I could get the GlobalID for some featureclasses/tables and not others. Consider the following code snippet.

fc = 'featureclass'
insertFields = ['field1', 'field2']
with arcpy.da.InsertCursor(fc, insertFields) as insertCursor:
   row = ['Blah', 'Blah']
   objectid = insertCursor.insertRow(row)

   searchFields = ['OBJECTID', 'GLOBALID']
   whereClause = 'OBJECTID = {0}'.format(objectid)
   with arcpy.da.SearchCursor(fc, searchFields, whereClause) as searchCursor:
       for row in searchCursor:
           globalid = row[searchFields.index('GLOBALID')]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For a featureclass I am trying to extract the GlobalID from the record that has just been inserted. This works for some of my featureclasses/tables but not others. After some testing (and hair pulling!) I have identified for the above code to work and return the globalid the featureclass has to participate in a composite relationship class with the globalid as the primary key. If the featureclass does not participate in a composite relationship class then the search cursor will not be able to find the previously inserted record.

If anyone has a better method of getting the globalid for a record that has just been inserted I would be interested in hearing from you.

cheers

Callum