AnsweredAssumed Answered

Python for checking field length?

Question asked by adailey_slo on Feb 27, 2020
Latest reply on Mar 3, 2020 by adailey_slo

I am trying to find an efficient way to check the maximum possible field length for many feature classes that are in datasets in an enterprise database system.  The problem I'm seeking to solve is that once in a while, a field within a feature class has a field length limit of over a billion characters. The actual data within the field is less than 255 characters, though. So I want to find the long fields and then I will edit the max length to 255.

 

I would love to use Python / arcpy to find the long fields. I have taken a class in Python but am not an expert, and I am brand new to using it in ArcGIS, so it would be great if you could explain things accordingly.    thank you!!

 

Here is the code I have so far.

It does begin with import arcpy - I forgot to include that in the screenshot.

 

 

Here it is in a form where you can copy and paste if you want to try it out with your own workspace:

import arcpy

arcpy.env.workspace = r'Database Connections\prvgisdb1-GIS-webadmin.sde'
data_sets = arcpy.ListDatasets()
for data_set in data_sets:
    print data_set # Datasets printed!
    FCs = arcpy.ListFeatureClasses(data_set)
    for FC in FCs:
        print(FC) # Feature classes did NOT print.
        fields = arcpy.ListFields(FC)
        for field in fields:
            if field.length > 255:
            print("{0} {1} {2}".format(field.name, field.type, field.length)) #Fields did NOT print.

 

It successfully connects to the database and prints the names of the datasets (this printing is for testing purposes).  However, nothing else printed.

I know the last half of the code does work, though - I have successfully gotten the long fields to print when I told it which feature class to look at. And there are indeed fields over 255 characters, so something should print.

Apparently there is some problem in this code where I have tried to put all the pieces together.

 

Any suggestions?

Thank you!!

Outcomes