4

I have to calculate a field based on another field in a joined table, but an error occurs:

"Parameters are not valid. ERROR 000728: Field !Table_A.field_A! does not exist within table"

My code:

import arcpy
from arcpy import env
env.workspace = "C:/TMP/BASELINE.gdb"
Table_A = "dist"
Table_B= "matrix"
arcpy.AddJoin_management(Table_A, "id_grid", Table_B, "id_matrix")
arcpy.CalculateField_management(Table_A, "!" + Table_A + ".field_A!", "!" + Table_B + ".field_B!","PYTHON")

Both tables are in the BASELINE.gdb. The Join occurs without any problem.

I forgot to say that, of course, "field_A" exists in "Table_A" and "field_B" exists in "Table_B".

Last but not least, if I run the same calculation in ArcMap, using Field Calculator, no error occurs.

migrated from stackoverflow.com Dec 25 '13 at 7:04

This question came from our site for professional and enthusiast programmers.

  • Ooops, too many bangs. The correct command is: arcpy.CalculateField_management(Table_A, Table_A + ".field_A", "!" + Table_B + ".field_B!","PYTHON") – albus2011 Nov 28 '13 at 13:45
7

Read the help for Add Join. In particular:

"The input must be a feature layer, a table view, or a raster layer that has an attribute table; it cannot be a feature class or table."

Of course, you are passing a table, but a join does not alter a table (i.e. the database), only a layer or a table view (i.e. in memory). That means by the time you call CalculateField_management, Table_A--a table or feature class--still does not have any more columns than it had before.

I think your best solution is to do what the sample in the help does: call MakeFeatureLayer_management first. If Table_A is just a table and not a feature class, call MakeTableView_management instead.

arcpy.MakeTableView_management(Table_A, "table_a_view")
arcpy.AddJoin_management("table_a_view", "id_grid", Table_B, "id_matrix")
arcpy.CalculateField_management(Table_A, Table_A + ".field_A", "!" + Table_B + ".field_B!","PYTHON")

Then "table_a_view" will be a table view, which means the join should work.

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.