Add Field decided to add an underscore to the field name

3179
8
Jump to solution
06-05-2012 10:19 AM
BenSloboda
New Contributor II
I created a script to build a template geodatabase.  The code creates a geodatabase, adds a few domains, creates a feature class, adds some fields to the feature class and sets their domains, and finally sets all the default values for the fields.

The problem is this one line of code:

arcpy.AddField_management(NewTrees, "Public", "TEXT", "", "", "", "", "", "REQUIRED", "Public")


Somehow, this code creates a field called "Public_".  Note the added underscore.  (NewTrees is just a variable referencing the newly created feature class.)

There are no other fields in the feature class before this line (as it was just created). 

Does anyone know why Python would add an underscore to a field name!?

-Ben S.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
When you add a field with a reserved name by code or through the tool, it automatically adds an underscore to it. In your desktop test, were you adding that field to a feature class stored in that particular geodatabase?

View solution in original post

0 Kudos
8 Replies
NobbirAhmed
Esri Regular Contributor
Does anyone know why Python would add an underscore to a field name!?


Could you please do the 'Add Field' process manually with the tool itself from ArcToolbox and see whether you still get the field name with an underscore?
0 Kudos
BenSloboda
New Contributor II
By using the tool within ArcDesktop, the add field tool properly added a field named "Public" with no underscore.

The following is the code snippet ArcDesktop used:

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "UTC_Parcels"
arcpy.AddField_management("UTC_Parcels","Public","TEXT","#","#","#","#","NULLABLE","REQUIRED","#")


This doesn't answer why Python would put an underscore on the field name, however.
0 Kudos
KenBuja
MVP Esteemed Contributor
What type of geodatabase are you creating the feature class? "Public" is deemed as a reserved word in some different types. See this link for a full list of the reserved words for the different RDBMS.
0 Kudos
JohnCobb1
New Contributor
Does your code add an underscore if you name the field something else? 'Public' sounds like a keyword. Considering you're setting domains there might be some conflict there, but that's just speculation on my part.
0 Kudos
BenSloboda
New Contributor II
The word "Public" does appear on the  ISO/ANSI SQL99 reserved words and the T-SQL reserved words list, but that doesn't really explain to me why Python changed it or why running the tool from within ArcDesktop worked fine...

I'm using an Enterprise SDE v.10 on SQL 2008 R2.

-Ben S.
0 Kudos
KenBuja
MVP Esteemed Contributor
When you add a field with a reserved name by code or through the tool, it automatically adds an underscore to it. In your desktop test, were you adding that field to a feature class stored in that particular geodatabase?
0 Kudos
BenSloboda
New Contributor II
No, my mistake.

[ATTACH=CONFIG]14959[/ATTACH]

Once I attempted on the same database, the identical results were evident.  So it looks like the RBDMS is to blame. 

Interesting turn of events, and I'd like to thank everyone for helping figure out what happened!

-Ben S.
0 Kudos
KenBuja
MVP Esteemed Contributor
Glad to help. Please mark the thread as answered.
0 Kudos