0

I am running arcpy.AddField_management to add a field called "cnid" and update it with x centroid values. For add.field I get the error "cnid" already exists. I tried print.field name and I cannot see "cnid" field. Now I am running calculate shape centroid and I get the error message "cnid" does not exist within table. I cannot understand, sometimes it works (if I close arcmap and open it again) but if I run both codes, add field and update it, it doesnt work.

    # AddField for new fields 
arcpy.AddField_management("Connections","cnid","DOUBLE", "", "", "", "", "NULLABLE")

    #test fields
    for field in arcpy.ListFields ("Connections"):
    ...     print field.name

    xExpression = "float(!SHAPE.CENTROID!.split()[0])" 
    inFeatures ="Connections"
    fieldName ="cnid"
    arcpy.CalculateField_management ("Connections","cnid",xExpression, "PYTHON")

I read something about in_memory and refresh bug from Why am I getting an ERROR 000728: Field *fieldname* does not exist within table even though field exists?, but how can I refresh it without having to close and open it again?

I am using ArcMap's Python window.

  • 1
    It looks like "Connections" is a layer, perhaps it would be better to add the field to the table the layer is from and remake the layer... possibly "in_memory\\connections". That part of the code isn't there. – Michael Stimson Dec 15 '14 at 3:05
  • "Connections" is feature class, I have just closed and open it again and "cnid" is there now. >:/ Where should I include "in_memory\\connections" in my code? thanks – JrUser Dec 15 '14 at 3:19
  • 1
    Is your arcpy.env.workspace = "in_memory"? Normally feature classes are given by their full path unless you're set to the workspace that they're in, layers (not layer files) don't have a path which is where it's a bit confusing. – Michael Stimson Dec 15 '14 at 3:28
  • Would you be able to edit your question to include all of a code snippet that you can run to see things working up to where you are stuck, and then the whole error message that is being returned when you run that code snippet, please? Also, let us know where you are running this code from e.g. IDLE, ArcMap's Python window, a Python script tool, or ...? – PolyGeo Dec 15 '14 at 3:43
  • 1
    When responding to requests for clarifications would you be able to use the edit button to revise your question body rather than contributing to a longer comment trail, please? How to ask a good question? is a useful reference on how best to attract potential answerers to your question. – PolyGeo Dec 15 '14 at 4:58
2

You have this problem because the field names to be used when you have a join are not the original field names. If you don't need the join anymore, you can use the "remove join" tool. If you do need the values from the joined table, you must include the table name in the field name, like this :

!table_name.field_name! 

If you want to make sure of the "spelling", try joining the table in ArcMap and run the field calculator : the joined field names will be listed.

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.