0

I needed to add a new field to a feature class to act as a unique key, but when I run:

fields = ['OID@', 'state', 'navkey']
ids = inspected_points #list of oid values
oidfield = arcpy.Describe(points).OIDFieldName #reference to ObjectID field name

where_clause_points = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(points, oidfield),tuple(ids))    

with arcpy.da.UpdateCursor(points, fields, where_clause_points) as cursor:

I get the error:

for row in cursor:
RuntimeError: Attribute column not found [42S22:[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'navkey'.] [db.DBO.lines_split][STATE_ID = 0]

I have been able to add and write to other fields, so I'm not sure why this one is not working. I know the field is there and available:

fields = arcpy.ListFields('lines_split')
    for field in fields:
    print(field.name)

This produces:

ProjectId
SurveyId
TechnicianUsername
TimestampUTC
GPSLatitude
GPSLongitude
GPSAccuracyInFeet
GPSAltitudeInFeet
Shape
OBJECTID
state
NEAR_FID
NEAR_DIST
NEAR_X
NEAR_Y
navkey

This field is just a text field. I just want to populate it with values, but cannot for some reason access it. The code above all works perfect if you remove the 'navkey' from the list of fields, but once you add it in it fails.

Ultimately I am looking to create and populate a field to contain GUIDS and act as a unique key, since you cannot rely on ObjectID or GlobalID as they can change and CopyFeatures does not copy the same keys. I have the python code to generate GUIDS as a string, but I need access to this new field to populate them.

  • I don't think you need to use the describe step before the addfielddelimiters. (oidfield = arcpy.Describe(points).OIDFieldName)The AddFieldDelimiters step can use the plain fieldname you listed. – smiller Jan 22 at 16:33
  • The reason I have that in there was an issue before on how to reference the ObjectID field-some feature classes have it OBJECTID, ObjectID, OID@...so I just grab what the database is calling it for that fc and use that. It adds complexity but works. – m.Walker Jan 22 at 16:35
  • Navkey isn't an object id field. What can you tell us about this field and how it was added? Did you create a unique identifier using the steps described in this link? desktop.arcgis.com/en/arcmap/10.3/manage-data/databases/… Is the field null or populated? – smiller Jan 22 at 16:37
  • nvkey is just a text field, length 255, nothing special about it. I created it in ArcGIS Pro using ArcCatalog. I am just trying to write a string value to it, like I did earlier in my code to a 'state' text field which works fine. I will ultimately use this as a unique key, but will not set it as such, but only refer to it and copy its value and use it as a key between feature classes. – m.Walker Jan 22 at 16:40
  • In that case I would definitely remove it from the tuple of fields that you are accessing using the oidfield definition, and simply access it by name. Tell us more about your where clause -- what are you trying to search? – smiller Jan 22 at 16:43
1

With the assistance of smiller, I was able to get that new field populated with the GUID values:

fields = ['OID@', 'state', 'navkey']
ids = inspected_points #list of oid values
oidfield = arcpy.Describe(points).OIDFieldName #reference to ObjectID field name
where_clause_points = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(points, oidfield),tuple(ids))    

with arcpy.da.UpdateCursor(points, fields, where_clause_points) as cursor:
    for row in cursor:
        pk = str(uuid.uuid4()) #generate a GUID
        row[2] = pk
        cursor.updateRow(row)

I actually added code at the beginning of my code that may have resolved this (possibly incorrectly worded/misleading) error:

where_clause = "[navkey] = '<Null>'"
field = ['navkey']

with arcpy.da.UpdateCursor(points, field, where_clause) as cursor: 
    for row in cursor:
        pk = str(uuid.uuid4())
        row[0] = pk
        cursor.updateRow(row)

This populated that new field with the generated GUID values, as strings, with no attribute could not be found error.

  • Please format your code by highlighting it and clicking the Format button. – PolyGeo Jan 22 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.