adding multiple fields to features in a geodatabase

1407
12
Jump to solution
04-30-2020 11:37 AM
TyquinWashington
New Contributor

I am having trouble trying to add multiple fields to polygon feature classes into a file geodatabase.

Here is the python script I am working with.

import arcpy
arcpy.env.overwriteOutput = True

#set the environment settings
arcpy.env.workspace = "Z:\\folder\\folder\\Practice\\Practice.gdb"

#Set local variables
fc = arcpy.ListFeatureClasses("Polygon")

#Main Loop to add fields to all Polygon feature classes in file geodatabase

for fc in arcpy.ListFeatureClasses():
    arcpy.AddField_management(fc, "sourceOFData", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.AddField_management(fc, "Source Last Edit Date", "DATE", "", "", "75", "True", "NON_REQUIRED", "")
    arcpy.AddField_management(fc, "uploadAuthority", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.AddField_management(fc, "effectiveDate", "DATE", "","", "75", "", "True", "NON_REQUIRED", "")
    arcpy.AddField_management(fc, "expirationDate", "DATE", "", "", "75", "", "True", "NON_REQUIRED", "")
    if fc == "DaviePSAP":
        arcpy.AddField_management(fc, "sourcePSAPUnqID", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
    elif fc in arcpy.ListFeatureClasses():
        arcpy.AddField_management(fc, "sourceUnqID", "TEXT", "", "", "254","", "NULLABLE", "NON_REQUIRED", "")
    elif fc in arcpy.ListFeatureClasses():
        arcpy.AddField_management(fc, "county", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "state", "TEXT", "", "", "2", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "country", "TEXT", "", "", "2", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "agencyID", "TEXT", "", "", "100", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceURI", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceURN", "TEXT", "", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceNumber", "TEXT", "", "", "15", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "agencyVCardURI", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "displayName", "TEXT", "", "", "60", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "comments", "TEXT", "", "", "100", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "gcLabel", "TEXT", "", "", "150", "", "NULLABLE", "NON_REQUIRED", "")
        
    

print "Fields have been added to all tables. Good Job!"

I am getting the following error:

Traceback (most recent call last):
  File "C:/Users/Desktop/AddField5.py", line 17, in <module>
    arcpy.AddField_management(fc, "Source Last Edit Date", "DATE", "", "", "75", "True", "NON_REQUIRED", "")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 3435, in AddField
    raise e
ExecuteError: ERROR 000622: Failed to execute (Add Field). Parameters are not valid.
ERROR 000800: The value is not a member of NULLABLE | NON_NULLABLE.

I believe the issue is related to the parameters that I have used for the "Date" fields I am attempting to create.  The current syntax works fine for fields that are just "TEXT" types.

Example

import arcpy
arcpy.env.overwriteOutput = True

#set the environment settings
arcpy.env.workspace = "Z:\\folder\\folder\\Practice\\Practice.gdb"

#Set local variables
fc = arcpy.ListFeatureClasses("Polygon")

#Main Loop to add fields to all Polygon feature classes in file geodatabase

for fc in arcpy.ListFeatureClasses():
    arcpy.AddField_management(fc, "sourceOFData", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
    #arcpy.AddField_management(fc, "Source Last Edit Date", "DATE", "", "", "75", "True", "NON_REQUIRED", "")
    arcpy.AddField_management(fc, "uploadAuthority", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
    #arcpy.AddField_management(fc, "effectiveDate", "DATE", "","", "75", "", "True", "NON_REQUIRED", "")
    #arcpy.AddField_management(fc, "expirationDate", "DATE", "", "", "75", "", "True", "NON_REQUIRED", "")
    if fc == "DaviePSAP":
        arcpy.AddField_management(fc, "sourcePSAPUnqID", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
    elif fc in arcpy.ListFeatureClasses():
        arcpy.AddField_management(fc, "sourceUnqID", "TEXT", "", "", "254","", "NULLABLE", "NON_REQUIRED", "")
    elif fc in arcpy.ListFeatureClasses():
        arcpy.AddField_management(fc, "county", "TEXT", "", "", "75", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "state", "TEXT", "", "", "2", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "country", "TEXT", "", "", "2", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "agencyID", "TEXT", "", "", "100", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceURI", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceURN", "TEXT", "", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "serviceNumber", "TEXT", "", "", "15", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "agencyVCardURI", "TEXT", "", "", "254", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "displayName", "TEXT", "", "", "60", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "comments", "TEXT", "", "", "100", "", "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(fc, "gcLabel", "TEXT", "", "", "150", "", "NULLABLE", "NON_REQUIRED", "")

This code works this way  but I really need the date field arguments to work in conjunction with the added text fields.  Any suggestions?

FYI: Still a novice when it comes to Python scripting.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

You could wrap your AddField with a try/except block. You could then print an error message if there is a problem creating the field.

for fc in arcpy.ListFeatureClasses():
    try:
        arcpy.AddField_management(fc, "sourceOFData", "TEXT", field_length="75")
    except:
        print "Error creating {} field".format('sourceOFData')

    try:
        arcpy.AddField_management(fc, "SourceLastEditDate", "DATE")
    except:
        print "Error creating {} field".format('SourceLastEditDate')

    # etc.

See Python Try Except for more information.

Another option would be to use ListFields to find out what fields are in your feature and then add those that are not already in the list.

Hope this helps.

View solution in original post

12 Replies
RandyBurton
MVP Regular Contributor

Looks like you have spaces (not allowed) in the field name: "Source Last Edit Date".  Since it is a date field, I also suggest leaving the length parameter blank.

TyquinWashington
New Contributor

I am still getting the same error even though I have removed the spaces from the field name and the value from the field length for my date fields.

0 Kudos
TyquinWashington
New Contributor

I have also restored replaced the True with NULLABLE and I am still getting the same error.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Although spaces are not allowed in field names, most geoprocessing tools will accept them.  What ends up happening behind the scenes is that the invalid name is made valid, likely using similar method as ValidateFieldName—ArcPy Functions | Documentation, and then the field is created and given the valid name while the invalid name with a space is made the alias of the field name.

0 Kudos
RandyBurton
MVP Regular Contributor

Also use NULLABLE or NON_NULLABLE, not "True" in your DATE lines.

0 Kudos
RandyBurton
MVP Regular Contributor

It also looks like you were missing the field alias.

#AddField(in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})
# arcpy.AddField_management(fc, "Source Last Edit Date", "DATE", "", "", "75", "True", "NON_REQUIRED", "")

arcpy.AddField_management(  fc, "SourceLastEditDate",    "DATE", "#", "#", "#", "FieldAlias", "NULLABLE", "NON_REQUIRED", "#")‍‍‍‍‍‍‍‍‍

# or

arcpy.AddField_management(fc, "SourceLastEditDate", "DATE",
                          field_is_nullable="NULLABLE",
                          field_is_required="NON_REQUIRED")‍‍‍‍‍‍‍‍‍
TyquinWashington
New Contributor

The reason that the field alias is absent is because for NEXTGen911, field aliases are not allowed for data upload.

0 Kudos
RandyBurton
MVP Regular Contributor

Since you are using the defaults for nullable and required, you could just omit those parameters.  Provide the first three required parameters in order, and add field_length=00 (some number) for your text fields.

0 Kudos
TyquinWashington
New Contributor

I used the second suggested Add field for Date and I am running into a overwrite error for one of my fields.  I set the overwrite environment so that it can overwrite fields.  What do you recommend?  I am writing this code as a practice run before I actually touch the true polygon feature classes.


Traceback (most recent call last):
File "C:/Users/folder/Desktop/AddField5.py", line 20, in <module>
arcpy.AddField_management(fc, "effectiveDate", "DATE", "#", "#", "#", "#", "NULLABLE", "NON_REQUIRED", "")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 3435, in AddField
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000012: effectiveDate already exists
Failed to execute (AddField).

0 Kudos