Howdy! My colleagues and I are having some difficulty trying to run the ListFields function against a feature dataset in an enterprise geodatabase that contains relationship classes. We are simply trying to export a list of all feature classes and their respective fields within every single feature dataset in a particular database. But it errors out on exclusively when ran against a feature dataset that contain relationship classes; if we run this against a feature dataset without any relationship classes, the code runs just fine. Below is the code we have:
import arcpy
import os
geodatabase_path = r"C:\PathTo\Database Connection.sde"
output_file_path = r"C:\Temp\Output.txt"
with open(output_file_path, "w") as f:
arcpy.env.workspace = geodatabase_path
datasets = arcpy.ListDatasets("*BadFeatureDataset*", "FeatureDataset")
f.write(f"Feature Dataset: {datasets}\n")
for ds in datasets:
for fc_fd in arcpy.ListFeatureClasses(feature_type='Polygon', feature_dataset=ds):
print (fc_fd)
f.write(f"Feature Class: {fc_fd}\n")
fields_fc_fd = arcpy.ListFields(fc_fd)
for field in fields_fc_fd:
f.write(f"{field.name}, {field.type}\n")
f.write("\n")
Traceback (most recent call last):
File "<string>", line 33, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 1214, in ListFields
return gp.listFields(dataset, wild_card, field_type)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 362, in listFields
self._gp.ListFields(*gp_fixargs(args, True)))
RuntimeError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
DBMS table not found [42S02:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'gisdata.OWNER.FeatureClassName'.] [gisdata.OWNER.FeatureClassName][STATE_ID = 108472]
Note that this is just sampling of the error; the full error lists all of the 20+ relationship class DBMS tables it cannot find (even though they exist and, from what I understand, the code is not even searching against). Plus, in our effort to troubleshoot, we simplified the code a bit to only run against the problematic feature dataset.
I'm wondering if it's a glitch we've stumbled upon. For reference we are running this on a machine with ArcGIS Pro 3.2.0 installed and have tested this with the Python Window and Notebook.
Solved! Go to Solution.
Try using arcpy.da.walk, its more robust.
Untested.
import arcpy
import os
geodatabase_path = r"C:\PathTo\Database Connection.sde"
output_file_path = r"C:\Temp\Output.txt"
with open(output_file_path, "w") as f:
# Use arcpy.da.Walk to navigate the geodatabase structure
for dirpath, dirnames, filenames in arcpy.da.Walk(geodatabase_path, datatype="FeatureClass"):
# Check if we're in a feature dataset
if geodatabase_path in dirpath and geodatabase_path != dirpath:
f.write(f"Feature Dataset: {os.path.basename(dirpath)}\n")
for filename in filenames:
fc_path = os.path.join(dirpath, filename)
print(filename)
f.write(f"Feature Class: {filename}\n")
try:
fields = arcpy.ListFields(fc_path)
for field in fields:
f.write(f"{field.name}, {field.type}\n")
except Exception as e:
f.write(f"Error reading fields for {filename}: {str(e)}\n")
continue
f.write("\n")
Try using arcpy.da.walk, its more robust.
Untested.
import arcpy
import os
geodatabase_path = r"C:\PathTo\Database Connection.sde"
output_file_path = r"C:\Temp\Output.txt"
with open(output_file_path, "w") as f:
# Use arcpy.da.Walk to navigate the geodatabase structure
for dirpath, dirnames, filenames in arcpy.da.Walk(geodatabase_path, datatype="FeatureClass"):
# Check if we're in a feature dataset
if geodatabase_path in dirpath and geodatabase_path != dirpath:
f.write(f"Feature Dataset: {os.path.basename(dirpath)}\n")
for filename in filenames:
fc_path = os.path.join(dirpath, filename)
print(filename)
f.write(f"Feature Class: {filename}\n")
try:
fields = arcpy.ListFields(fc_path)
for field in fields:
f.write(f"{field.name}, {field.type}\n")
except Exception as e:
f.write(f"Error reading fields for {filename}: {str(e)}\n")
continue
f.write("\n")
This worked! Just tested it and it produced the results we were seeking. 👍👍
Your script is working as expected for me.
I tested on machine with Pro 3.2.1 and one with Pro 3.1.3, both connecting to SQL server sde 10.9.1.2.9 database.
Not all the featureclasses in my feature dataset have relationship classes, but many do and they get reported just the same.
R_
Very odd, I tried my original code on our test enterprise environment and got the same results. Both are 10.9.1 enterprise geodatabases.
I also stood up a non enterprise file geodatabase with a feature dataset with one feature class and a relationship class to a standalone table. That did not fail.
Oh and just for reference, I tested the original problematic code on another machine running Pro 2.9.0 against the 10.9.1 enterprise geodatabases and it still produced the same error.
that is weird, something different in the data I guess.
This is what I used:
import arcpy
geodatabase_path = r"Database Connections\PublicWorks.sde"
output_file_path = r"C:\Temp\Output.txt"
with open(output_file_path, "w") as f:
arcpy.env.workspace = geodatabase_path
datasets = arcpy.ListDatasets("*Storm_Features*", "Feature")
f.write(f"Feature Dataset: {datasets}\n")
for ds in datasets:
for fc_fd in arcpy.ListFeatureClasses(feature_type='Polygon', feature_dataset=ds):
print (fc_fd)
f.write(f"Feature Class: {fc_fd}\n")
fields_fc_fd = arcpy.ListFields(fc_fd)
for field in fields_fc_fd:
f.write(f"{field.name}, {field.type}\n")
f.write("\n")
And this is what came out (I removed all but the two sdStructure FC's in the output.txt file, one with and one without relationship class:
Thought maybe it was because of "FeatureDataset" in the ListDatasets call, but seems to work the same with that or "Feature".
R_