Unable to register db view on UtilityNetwork feature classes

257
0
12-09-2020 10:56 PM
AllanSimonsen
New Contributor

I'm having trouble registering a database view with the geodatabase when the view contains features classes from the utility network.

Basically I have a script that creates a database view using the arcpy.CreateDatabaseView_management(...) method and then I would like to have the view registered with the geodatabase using the arcpy.RegisterWithGeodatabase_management(...) method. The create view part of the script works as intended, but the call to RegisterWithGeodatabase_management fails with the message: 'The column must not have a NOT NULL constraint'.

I have tried to remove the OBJECTID from the view definition, but then I get the error message: 'The current table doesn't have an ArcGIS-maintained row_id column'

I cannot see what i'm doing wrong here. Any ideas would be very much appreciated.

This is the entire output of the script:

View created: MyTestView_ElectricDistributionDevice
RegisterWithGeodatabase parameters - OIDFieldName: OBJECTID, shapeType: Point, shapeFieldName: SHAPE
Failed to register view with geodatabase: SXM_WorkOrder_ElectricDistributionDevice
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.
The column must not have a NOT NULL constraint
Failed to execute (RegisterWithGeodatabase).

Below is the script I have reduced my own script to illustrate the issue. I'm using a sde file as the databaseCommention parameter and the db user I have created the database with.

# -*- coding: utf-8 -*-
import arcpy

def AddViewAndRegister(databaseConnectionPath, dbUser):

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False
    tableName = "ElectricDistributionDevice"
    viewDefinition = f"""SELECT
        un.OBJECTID,
        un.ASSETGROUP,
        un.ASSETTYPE,
        un.SHAPE,
        un.GLOBALID,
        un.nameasset,
        un.nameassetowner
    FROM {dbUser}.{tableName} AS un"""

    # Process: Create Database View (Create Database View)
    viewName = f"MyTestView_{tableName}"
    if arcpy.Exists(f"{databaseConnectionPath}\\{viewName}"):
        print(f"View already exists: {viewName}")
        arcpy.Delete(f"{databaseConnectionPath}\\{viewName}") # For debugging only
        print(f"View Deleted: {viewName}") # For debugging only
        return

    myNewView = arcpy.CreateDatabaseView_management(
        input_database=databaseConnectionPath,
        view_name=viewName,
        view_definition=viewDefinition)[0]
    print(f"View created: {viewName}")

    tableDescription = arcpy.Describe(f"{databaseConnectionPath}\\UNDataSet\\{tableName}")
    objectId_field = tableDescription.OIDFieldName
    shape_field = tableDescription.shapeFieldName
    shape_type = tableDescription.shapeType
    print(f"RegisterWithGeodatabase parameters - OIDFieldName: {objectId_field}, shapeType: {shape_type}, shapeFieldName: {shape_field}")

    erpDataDescription = arcpy.Describe(f"{databaseConnectionPath}\\UNDataSet")
    spatial_ref = tableDescription.spatialReference

    try:
        # Process: Register with Geodatabase (Register with Geodatabase)
        arcpy.RegisterWithGeodatabase_management(
            in_dataset=myNewView,
            in_object_id_field=objectId_field,
            in_shape_field=shape_field,
            in_geometry_type=shape_type,
            in_spatial_reference=spatial_ref,
            in_extent="0 6000000 1500000 8000000"
        )
        print(f"View registered with geodatabase: {viewName}")
    except Exception as ex:
        print(f"Failed to register view with geodatabase: {viewName}")
        print(ex)

if __name__ == '__main__':
    databaseConnectionPath = sys.argv[1]
    dbUser = sys.argv[2]
    # print("databaseConnectionPath:" + databaseConnectionPath)
    with arcpy.EnvManager(scratchWorkspace=databaseConnectionPath, workspace=databaseConnectionPath):
        AddViewAndRegister(databaseConnectionPath, dbUser)

 

0 Kudos
0 Replies