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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 28, 2013 at 11:25
1
  • Ooops, too many bangs. The correct command is: arcpy.CalculateField_management(Table_A, Table_A + ".field_A", "!" + Table_B + ".field_B!","PYTHON") Commented Nov 28, 2013 at 13:45

1 Answer 1

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Dec 2, 2013 at 18:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.