AnsweredAssumed Answered

Bug?: File GDB: Add Join, SelectbyAttribute, Calculate Field

Question asked by ehm119 on Oct 12, 2012
Latest reply on Oct 17, 2012 by ehm119
I am having a problem with a file geodatabase join (2 feature classes) that is not happening when I use shapefiles.  I am using ArcGIS 10 SP5.  Here is my problem & workflow.


  1. Take Feature Class 1 and add a new field

  2. calculate that field to equal an existing field

  3. join the feature class to Feature Class 2

  4. use select by attributes to create a selection

  5. calculate the new field to equal something else, for only the selected records.  The new value is the sum of 2 fields of the same name in the 2 feature classes

  6. clear the selection and remove the join

The problem is that after I calculate the new field to equal something new, all of the old values (calculated in step 2) are set to null for records that do not join.  Here is my code:

import arcpy      Workspace = r"C:\Users\me\Desktop\test.gdb" arcpy.env.overwriteOutput = True arcpy.env.workspace = Workspace  FC = "PointFC1" FC2 = "PointFC2"   arcpy.AddField_management(FC, "newAttributeX", "DOUBLE") arcpy.CalculateField_management(FC, "newAttributeX", '[AttributeX]')  arcpy.MakeFeatureLayer_management(FC, "FC_lyr") FC_lyr = "FC_lyr"  #Join FCs and create selection arcpy.AddJoin_management(FC_lyr, "JoinField", "FC2", "JoinField") selection = '"%s.OBJECTID" IS NOT NULL' %FC2 arcpy.SelectLayerByAttribute_management(FC_lyr, "NEW_SELECTION", selection )  #Calc values for newAttributeX that join to second FC expression = "[%s.AttributeX] + [%s.AttributeX]" %(FC, FC2) arcpy.CalculateField_management(FC_lyr, "%s.newAttributeX" %FC, expression)   arcpy.SelectLayerByAttribute_management(FC_lyr, "CLEAR_SELECTION") arcpy.RemoveJoin_management (FC_lyr, "FC2")


Again, everything up to the selection is working properly... the problem is that all the null values (from those records that didn't join) are copied over to the new field, when in fact the field calculation should only be working on the selected records.

A viable work around is to wait to calculate the old values until the end:

  1. Take Feature Class 1 and add a new field

  2. join the feature class to Feature Class 2

  3. use select by attributes to create a selection

  4. calculate the new field to equal something else, for only the selected records.  The new value is the sum of 2 fields of the same name in the 2 feature classes

  5. clear the selection and remove the join

  6. select the values in the new field that didn't join

  7. calculate the null values in the new field to equal the values an existing field

However, the behavior I am getting is contrary to the documentation -- only the selected records should be calculated.  And as i mentioned above, this problem is not happening when I use 2 shapefiles.

Anyone else come across this problem?
-Erik

Outcomes