Datetime.now() No Longer Working Python 3.7.9

3444
13
Jump to solution
01-28-2021 01:07 PM
Labels (1)
LeviCecil
Occasional Contributor III

I have a script that automatically updates a sync enabled sde feature class. I populate the last_edited_date field with datetime.now(). This has worked for over a year until last week. All of a sudden I'm getting this error RuntimeError: The value type is incompatible with the field type. [last_edited_date]. Not sure what's going on. The date field in the feature class is in this format: %m/%d/%Y %H:%M%p or 1/28/2021 1:05 PM. 

Datetime.now() prints this format: 2021-01-28 13:06:30.180737

Tags (1)
0 Kudos
13 Replies
Robert_LeClair
Esri Notable Contributor

Thx Levi - I agree with Dan that I think this is on the RDBMS/Server side.  What release of SQL Server are you using?  The only related item I found on the "internals" was for Oracle - could be related or not.  See below for the Oracle resolution:

"After researching I found the reason for the error and a possible solution. Beginning with ArcGIS 10.4 we started to notice a problem making selections on the date fields. The problem was a wrong SQL statement. The solution was adding "TO_CHAR" before the date field in the select query. This addition converts the date field into a string that works with our software. I suggest adding "TO_CHAR(vc.AdmitDt)," to your select query and checking if the selection works before running the insert. Attached is a screen capture of how the select script should look. Also see Oracle document for better formatting instructions."

LeviCecil
Occasional Contributor III

We're on SQL Server 2019

0 Kudos
LeviCecil
Occasional Contributor III

So even weirder, the script is actually updating the feature with the current datetime, but I'm still getting that error before the script finishes running:

edit.PNG

edit2.PNG

Here is my script:

 

import requests
import json
import os, sys
import arcpy
import datetime

##This script checks Tririga for modifications to spaces/rooms in the last week.
##If modifications are found, space attributes are updated in the
##Rooms with Space ID sde feature class.

datetime = datetime.datetime.now()

##Query Tririga for spaces with recent modifications
tririga_mods_url = "https://<tririgaserver>/oslc/spq/PPSspaceModQC?oslc.select=*"
payload = {'USERNAME': 'user', 'PASSWORD': 'password'}
request = requests.get(tririga_mods_url,params=payload)
result = json.loads(request.text)
##print(json.dumps(result, indent=2)) ##prettify json
##sys.exit()
jsonData = result["rdfs:member"]

triSystemID_List = ['10423582',]

##Append Tririga System IDs to empty list

for entry in jsonData:
    triSystemID = entry['spi:identifier']
    triSystemID_List.append(triSystemID)
##print(triSystemID_List)
##sys.exit()

if len(triSystemID_List) == 0:
    print("No recent space modifications")
    sys.exit()

else:
    
    ##Query Tririga for modifications in each space
    for SystemID in triSystemID_List:

        tririga_space_query_url = "https://<tririgaserver>/oslc/so/PPSspaceGIS/" + SystemID 
        request = requests.get(tririga_space_query_url,params=payload)
        result = json.loads(request.text)

        ##Tririga attributes from result dict:
        triSpaceClass = result["spi:triCurrentSpaceClass-triNameTX"]
        triSpaceClassDesc = result["spi:triCurrentSpaceClass-triDescriptionTX"]
        triSpaceID = result["spi:triIdTX"]
        triCapacity = result["spi:triDefaultCapacityNU"]
        
        triParentFloor = result["spi:triParentFloorTX"]
        triParentBuilding = result["spi:triParentBuildingTX"]
        triUsedForK2 = result["spi:cstCurrentlyUsedForK2BL"]
        triParentProperty = result["spi:triParentPropertyTX"]
        triArea = result["spi:triAreaNU"]
        triMeetsK2Criteria = result["spi:cstMeetsCriteriaForK2CommonBL"]
        if triMeetsK2Criteria == 1:
            triK2Compliant = 'TRUE'
        elif triMeetsK2Criteria == 0:
            triK2Compliant = 'FALSE'
        else:
            triK2Compliant = None
        triAltSpaceUse = result["spi:cstAlternativeSpaceUseCL"]
        triRoomNum = result["spi:triNameTX"]
        
        triParentID = result["spi:triParentIdSY"]
        triSystemID = result["spi:identifier"]

        ##connect to sync enabled sde feature class on ArcGIS server
        arcpy.env.workspace = r"C:\Connections\PPS_Enterprise_Prod.sde" 
        arcpy.env.overwriteOutput = True
        rooms_web = "PPSProd.DBO.Rooms_w_SpaceID_All_Sites_WEB"
        rooms_harn = "PPSProd.DBO.Rooms_w_SpaceID_All_Sites"
        rooms_list = [rooms_web,rooms_harn]
        
        ##Only find spaces in the feature class that match the Tririga SpaceID field.
        where_clause = "WHERE SpaceID = '" + triSpaceID +  "'"

        for fc in rooms_list:
            
            with arcpy.da.UpdateCursor(fc, '*', where_clause) as cursor:

                ##sde feature class attributes 'fc'
                
                for row in cursor:


                    fcSpaceID = row[2]
                    fcSiteName = row[5]
                    fcRoomNum = row[6]
                    fcK2Compliant = row[7]
                    fcSpaceClass = row[8]
                    fcArea = row[9]
                    fcEditUser = row[13] ## last_edited_user field
                    fcEditDate = row[14] ## last_edited_date field
                    fcRoomDescription = row[16]
                    fcAlternateUse = row[17]
                    ##fcCapacity = row[18]
                  
            
                ##update feature class with attrubute data from Tririga 'tri'

                    if (row[:20] != [triRoomNum,triSpaceClass,triArea,
                                     triK2Compliant, ##triCapacity,
                                     triAltSpaceUse,triSpaceClassDesc]):
                        row[6] = triRoomNum
                        row[7] = triK2Compliant
                        row[8] = triSpaceClass

                        if fcArea - triArea > 1.0 or fcArea - triArea <= -1.0 :
                            row[9] = triArea
                        else:
                            row[9] != triArea
                       
                        row[13] = "GIS Admin"
                        row[14] = datetime
                        row[15] = triSpaceClassDesc
                        row[16] = triAltSpaceUse
                        ##row[18] = triCapacity

                        print("Updating " + triParentProperty + " Room Number " + triRoomNum + " in " + fc)
                        cursor.updateRow(row)

 

0 Kudos
LeviCecil
Occasional Contributor III

Finally figured this out. I'm updating two nearly identical datasets, one for the web and one in local projection for printed maps. The local dataset lacks a Global ID field, so the indexs were off by one and the script was trying to put a date in a non-date field. It's strange that this hasn't been an issue before.

0 Kudos