adding multiple fields to features in a geodatabase

3262
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
12 Replies
RandyBurton
MVP Alum

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.

TyquinWashington
New Contributor

Thank you very much. I did the try/except block and it works for everything. I really appreciate all the help you gave to me.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you have solved or found a workaround for your issue, please mark someone's reply as correct or mark the thread as assumed answered to close it out.

0 Kudos