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:
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]
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)
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
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.