#define the parent table Table2 = r"database connections\connection to db\db.DBO.Table1__ATTACH" #create a cursor on the child table's record that is linked to the parent table's record on the non-ESRI keys Rows = arcpy.UpdateCursor("db.DBO.Table1__ATTACH", "select * from db.DBO.Table1 where db.DBO.Table1.PK_GUID = db.DBO.Table1_ATTACH.FK_GUID", "", "REL_OBJECTID") for row in rows: row.REL_OBJECTID = T2.OBJECTID
Solved! Go to Solution.
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fc = "Airports" table = "Airports__ATTACH" # Add a field to the feature class and calculate the OBJECTIDs to this new field arcpy.AddField_management(fc, "OBJECTID2", "LONG") arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3") # Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join) arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2") # Update the REL_OBJECTID field with the OBJECTID2 values rows = arcpy.UpdateCursor(table) for row in rows: row.REL_OBJECTID = row.OBJECTID2 rows.updateRow(row) del row, rows # Delete the OBJECTID2 fields from the feature class and table arcpy.DeleteField_management(fc, "OBJECTID2") arcpy.DeleteField_management(table, "OBJECTID2")
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fc = "Airports" table = "Airports__ATTACH" # Add a field to the feature class and calculate the OBJECTIDs to this new field arcpy.AddField_management(fc, "OBJECTID2", "LONG") arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3") # Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join) arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2") # Update the REL_OBJECTID field with the OBJECTID2 values rows = arcpy.UpdateCursor(table) for row in rows: row.REL_OBJECTID = row.OBJECTID2 rows.updateRow(row) del row, rows # Delete the OBJECTID2 fields from the feature class and table arcpy.DeleteField_management(fc, "OBJECTID2") arcpy.DeleteField_management(table, "OBJECTID2")
Hi Matt,
It's actually a little tricky to calculate field values from one table to another outside of ArcMap. Here is some sample code that I was able to get to work:import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" fc = "Airports" table = "Airports__ATTACH" # Add a field to the feature class and calculate the OBJECTIDs to this new field arcpy.AddField_management(fc, "OBJECTID2", "LONG") arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3") # Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join) arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2") # Update the REL_OBJECTID field with the OBJECTID2 values rows = arcpy.UpdateCursor(table) for row in rows: row.REL_OBJECTID = row.OBJECTID2 rows.updateRow(row) del row, rows # Delete the OBJECTID2 fields from the feature class and table arcpy.DeleteField_management(fc, "OBJECTID2") arcpy.DeleteField_management(table, "OBJECTID2")
import arcpy from arcpy import env env.workspace = "database connections\connection to db\Fire.DBO" fc = "Event" table = "Event__ATTACH" # Add a field to the feature class and calculate the OBJECTIDs to this new field print "attempting to add field..." arcpy.AddField_management(fc, "OBJECTID2", "LONG") print "added field 'OBJECTID2'..." arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3") print "Calculated field..." # Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join) arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2") # Update the REL_OBJECTID field with the OBJECTID2 values rows = arcpy.UpdateCursor(table) for row in rows: row.REL_OBJECTID = row.OBJECTID2 rows.updateRow(row) del row, rows # Delete the OBJECTID2 fields from the feature class and table arcpy.DeleteField_management(fc, "OBJECTID2") arcpy.DeleteField_management(table, "OBJECTID2")
env.workspace = "database connections\connection to db.sde"
Try changing your workspace to the following:env.workspace = "database connections\connection to db.sde"
You are missing the '.sde' for the connection file, and you will not have to specify the database name and owner. Python will use the credentials stored within the SDE connection file.