Select to view content in your preferred language

Calculate a GUID Field with a value

5637
6
08-10-2018 02:56 PM
JoeBorgione
MVP Emeritus

Is it possible to calculate a GUID type field with a value of my own design?  I can create a string of the specified dimensions with my own string + blank spaces and brackets:

with arcpy.da.SearchCursor('tv',"*") as cursor:
     for row in cursor:
         oid = row[0]
        select = 'OBJECTID = {}'.format(oid)
         arcpy.SelectLayerByAttribute_management("tv","NEW_SELECTION",select)
         length = len(row[1])
         target = 36 - length
         value = row[1].ljust(target,' ')
         value = '{'+ '{}'.format(value)
         value = value+'}'
         print value
...         
{BC_00.61                    }

But when I plug in a arcpy.FieldManagement() method instead of  the print in line 11, I get the famous error # 999999: 'generic error'....

That should just about do it....
Tags (1)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

If you really need to know why Joe... I can explain it, but try

value = "BC_00.61               "
v = "{{{}}}".format(value)

v
'{BC_00.61               }'
JoeBorgione
MVP Emeritus

That's really cool Dan.  I think I get it: inner most pair is for the format(); second pair provide the curly brackets; outer most pair provide the single quote.  Right?   

I'll give that a try when I return to the office Monday.  So you think I can fake a GUID with this?  (Also been looking at uuid methods)

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

The inner-most {} is the standard format thing.  To show a { or } you need to 'escape' those using curly braces instead of backslash like in file paths (ie "c:\\Temp")

So put it together you get

"{}".format("stuff")  # ---- normal
'stuff'

"{{}}".format()  # ---- just the brackets
'{}'

"{{{}}}".format("stuff")  # ---- put it together
'{stuff}'‍‍‍‍‍‍‍‍

whether it does the GUID etc, you can test, 😉

JoeBorgione
MVP Emeritus

GUIDs don't like to be messed with: can't fake them.  Oh well....

edited:

Just to follow up on GUIDs versus GlobalIDs: they are similar in nature yet very different at the same time:

You can add a GUID field, but YOU need to populate it

When a GlobalID field is added, the geodatabase generates it automagically

If you add a record to a GUID enabled table or feature class, you will need to calculate the GUID value

If you add a record to a GlobalID enabled table or feature class, the geodatabse will automagically generate a new value

If you migrate GUID enabled data from one geodatabase to another gdb, the GUID value WILL PERSIST.

If you migrate GlobalID enabled data from one geodatabase to another gdb, the GUID value WILL NOT PERSIST.

Populating the GUID field—Help | ArcGIS Desktop 

How To: Calculate unique identifier values similar to Global IDs 

( more in the python module, uuid)

That should just about do it....
JoeBryant1
Frequent Contributor

Joe, thanks for the explanation of GUIDs versus GlobalIDs. With that in mind, why use a GUID field instead of a standard long integer of string field, since the geodatabase is not automatically populating it for you? Is this to get around requirements REST services have requiring a field of type GUID (but not necessarily GlobalID)? For instance, it is recommended to use a GUID field for the key in relationships between Survey123 inspection records and the assets they are inspecting.

0 Kudos
JoeBorgione
MVP Emeritus

Joe-  you hit the nail on the head with: For instance, it is recommended to use a GUID field for the key in relationships between Survey123 inspection records and the assets they are inspecting.  The person I was working with wanted to see if we could get away from the Global_ID, Parent_GlobalID relationship.  Survey123 doesn't like anything else.

Personally, I'm with you when creating key fields; in the past, I've added a field called 'JoinID' and calc it to equal the ObjectID.  That way it persists even if you change databases and you have  unique values.  Of course if you do change databases, and need to update that JoinID because of new records, there's a little work involved...

Joe Bryant

That should just about do it....