Preserve GlobalIDs with arcpy.da.InsertCursor

514
6
11-23-2023 03:26 PM
Labels (3)
EM-wsp
by
New Contributor II

Hi,

Is it possible to preserve the GlobalIDs using arcpy.da.InsertCursor()?

I have an SDE database, arcpy.env.preserveGlobalIds = True and when I use Append to copy the data, GlobalIDs come through and match the source datasetas they should. 

However, if I use arcpy.da.InsertCursor(), GlobalIDs get replaced with the new ones.

Does preserveGlobalIds setting work just for Append? Is it the way to make the Cursor respect this setting?

Thank you!

Tags (3)
0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

An overview of geoprocessing environment settings—ArcGIS Pro | Documentation

I think the operative word is "Tools".  arcpy.da is a package and has no tools associated with it.

making a copy of existing values would at least provide you with a backup.

 

Geodatabase Advanced environments
Environment Description
Preserve Global IDs

Tools that honor the Preserve Global IDs environment will reuse the Global ID values of the existing input dataset when appending rows to a target dataset.


... sort of retired...
0 Kudos
EM-wsp
by
New Contributor II

Hi!
Thank you  for your reply! I understand now why environmental setting is ignored by the Cursor.

But is there any way to get globalids preserved with the InsertCursor? I tried to apply UpdateCursor as well after inserting, but with no success. The tool doesn't complain or fail, it even takes some time to run, but the GlobalIDs are still new.

Since the in-built tools (Append, Copy etc) can have it preserved, the database apparently has this functionality. Can it be somehow set up in the Cursor functions?

0 Kudos
DanPatterson
MVP Esteemed Contributor

No is the short answer.  Geoprocessing Tools have environments


... sort of retired...
0 Kudos
EM-wsp
by
New Contributor II

Ok, thank you. At least I got it confirmed.

0 Kudos
VinceAngelo
Esri Esteemed Contributor

The long answer is that you might be able to use the fid returned by arcpy.da.InsertCursor.insertRow() to build a dictionary (lookup[fid] = old_globalid), then use the created fid in a DA UpdateCursor pass to update the uuid value:

 

updated = 0
with arcpy.da.UpdateCursor(fc,['objectid','globalid']) as cursor:
    for row in cursor:
        if row[0] in lookup:
            cursor.updateRow([row[0],lookup[row[0]])
            updated += 1
print("{:d} globalid values updated".format(updated))

 

This could only work because cursors ignore GlobalID values. I haven't tested this, but it's worth a shot.

- V

PS: Oh, and there exists a chance (however slight) that one of the new UUIDs could conflict with  a replacement GlobalID value, though it's  so slight that you probably don't need to check first (but if it does occur, then the global dictionary in memory will be lost by the error).

0 Kudos
EM-wsp
by
New Contributor II

Hi,

Thank you for your reply.

I had tested exactly the same approch before creating this post, and it didn't work. We can conclude that neither InsertCursor, nor UpdateCursor are able to modify GlobalIDs.