Return the Object ID from the Insert Row cursor

4156
7
04-28-2011 02:03 PM
deleted-user-1T_bOHag6M8d
Occasional Contributor
How can I return the object id of a row inserted with the InsertCursor? I've tried to use the script below, but it returns an error.

tbl = r"C:\temp\temp.gdb\test_table"
rows = arcpy.InsertCursor(tbl)
row = rows.newRow()
row.NAME = "TestName"
rows.insertRow(row)

print row.OBJECTID #This returns the following error: Runtime error <type 'exceptions.RuntimeError'>: Row: Field OBJECTID does not exist

print row.NAME #This returns the same error for the NAME field even though I just set the value and the new row is definitely inserted in the table.

## I've also tried the following with no luck
print row.getValue("OBJECTID")
print row.getValue("NAME")
print row.getValue(row.OBJECTID)
print row.getValue(row.NAME)


Thanks.
Tags (2)
0 Kudos
7 Replies
ChrisSnyder
Regular Contributor III
Nope, as far as my attempts have gone, you can't do it (retrieve an insert cursor value like you can with an update cursor).

So a v9.3 example:
insertRows = gp.insertcursor(myTable)
insertRow = insertRows.newrow()
insertRow.MYFIELD = "my value"
print insertRow.MYFIELD #THIS BOMBS!


It's a bug.
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
Nope, as far as my attempts have gone, you can't do it (retrieve an insert cursor value like you can with an update cursor).

So a v9.3 example:
insertRows = gp.insertcursor(myTable)
insertRow = insertRows.newrow()
insertRow.MYFIELD = "my value"
print insertRow.MYFIELD #THIS BOMBS!


It's a bug.


Thanks, Chris. I'll submit a bug report.
0 Kudos
ChrisSnyder
Regular Contributor III
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
BTW: this is not a new issue: http://forums.esri.com/Thread.asp?c=93&f=1729&t=293522


Again, thanks. I'll try the workaround posted at the bottom of that thread and hold out hope that it'll be a bug that is fixed quickly.
0 Kudos
ChrisSnyder
Regular Contributor III
It would seem logical (but perhaps incorrect) to assume that the row(s) you are inserting will always have ordered and consecutive OID values relative to any existing OID values in the table. So, if inserting rows into an empty table, logically your fist inserted row would be OID = 1 (OID = 0 if a shapefile/dbf). Similarly, if inserting rows into a table with existing records (last existing one ending in OID = 222), then your first inserted row would be OID = 223. Note that the insertcursor method does not have a sort order parameter, so no chance for that messing with an otherwise orderly universe. So maybe a faster/better work around would be prior to running an insert cursor, run a searchcursor to grab the largest existing OID value, and in your insertcursor, start counting at that number.
0 Kudos
ChrisSnyder
Regular Contributor III
hope that it'll be a bug that is fixed quickly

:rolleyes:
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
This was filed as a bug by ESRI. The number is #NIM067843.
0 Kudos