Hello. When I try to create a new child version using the ArcGIS Python API to write some updates, I receive this message below. I wanted to know if anyone has successfully created a temporary child version, applied edits, reconciled/posted the updates to the parent, and deleted the child version in the process.
Error creating version: Failed to execute. Parameters are not valid.
ERROR 000837: The workspace is not the correct workspace type.
WARNING 000379: The value may not be valid because of empty domain or does not exist in the domain
Failed to execute (CreateVersion).
from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Feature
import arcpy
import time
# Record the start time
start_time = time.time()
# Connect to the GIS
gis = GIS(url="YOUR PORTAL URL", username="YOUR USERNAME", password="YOUR PASSWORD")
# Path to the SDE file (i.e., the database connection file)
database = r"P:\GIS.sde"
# Define version names
parent_version = "sde.DEFAULT"
child_version = "Child_Version"
# Try to create the child version
try:
arcpy.CreateVersion_management(database, parent_version, child_version, "PUBLIC")
print(f"Created version: {child_version}")
except Exception as e:
print(f"Error creating version: {e}")
arcpy.AddError(e)
sys.exit() # stop the script if version creation fails
# Create a feature layer from the feature class
feature_layer = arcpy.management.MakeFeatureLayer(feature_class, "FeatureLayer").getOutput(0)
# Switch to the new version
arcpy.ChangeVersion_management(feature_layer, "TRANSACTIONAL", f"{child_version}")
# Get the feature service
service_id = "SERVICE ID"
service = gis.content.get(service_id)
layer = service.layers[0]
# Fetch the features to be updated
features = layer.query() # You may need to refine this query
# Create a dictionary to match the prefixes to the correct "St_PreTyp" value
prefixes = {
"Interstate": "Interstate"
}
# Loop through the features
for feature in features:
# Get the "St_Name" value
st_name = feature.attributes["St_Name"]
if st_name is not None:
update_required = True
# Check if St_Name contains only the Spanish prefix
if st_name.strip() in ["Calle"]:
update_required = False # No update required
# If update is required, loop through the prefixes to find a match
if update_required:
for prefix, pretyp in prefixes.items():
if st_name.startswith(prefix):
# If additional characters are present, parse and update the fields
feature.attributes["St_PreTyp"] = pretyp
feature.attributes["St_Name"] = st_name.replace(prefix, "").lstrip().replace("Highway", "").lstrip()
break
else:
feature.attributes["St_PreTyp"] = None
# Submit the edits
layer.edit_features(updates=features)
# After performing your operations, reconcile and post the changes from the child version to the parent version
arcpy.ReconcileVersions_management(database, "ALL_VERSIONS", parent_version, child_version, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION")
# Switch back to the parent version
arcpy.ChangeVersion_management(database, "TRANSACTIONAL", parent_version)
# Delete the child version
arcpy.DeleteVersion_management(database, child_version)
# Record the end time
end_time = time.time()
# Calculate the total time in minutes
total_time = (end_time - start_time) / 60
print("Successfully updated parsing for feature class")
print("Total time taken: {:.2f} minutes".format(total_time))