Select to view content in your preferred language

Is it Possible to Set a Default GDB via Arcpy?

1301
6
03-06-2023 05:01 PM
rescobar
New Contributor III

I have tried to write a script to save a copy of a aprx, while changing the default gdb, using the documentation I found at the link below.

aprx.defaultGeodatabase = .... 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm

However, this doesn't actually work and the resulting aprx just has the same default gdb as the original aprx I have open. Is there something I'm missing?

import arcpy, os
from arcpy import env
proj_folder = arcpy.GetParameterAsText(0)
outfolder = arcpy.GetParameterAsText(1)
aprx_name = arcpy.GetParameterAsText(2)
aprx = arcpy.mp.ArcGISProject("CURRENT")
new_gdb = proj_folder + "\\Data\\ProjComp\\ProjComp.gdb"
aprx.defaultGeodatabase = new_gdb
aprx.saveACopy(outfolder+"\\"+aprx_name)

 

 

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

There was a cavaet in the link

defaultGeodatabase
(Read and Write)
The project's default geodatabase location. The string must include the geodatabase's full path and file name. Note: this property can be set as read-only by an Administrator and therefore it can't be modified.

String

ArcGISProject example 1 shows basically what you have done above, but I wonder if you have to make outfolder and proj_folder the same so that the aprx and gdb are in the same relative positions as the "CURRENT" project


... sort of retired...
0 Kudos
rescobar
New Contributor III

Thanks, it seems like this yields the same results

0 Kudos
ChristopherRoss8
New Contributor

I have had the same experience. The default geodatabase can be set/defined but once you open the aprx you need to add the geodatabase as a database connection. It will then show as the default geodatabase. This is the only workaround I presently am familiar with.

0 Kudos
rescobar
New Contributor III

Thanks, I take it there's no way to add a database connection in arcpy then?

0 Kudos
ChristopherRoss8
New Contributor

Not that I am aware of.

0 Kudos
DanPatterson
MVP Esteemed Contributor

There is updateFolderConnections in arcpy's mapping class, but it doesn't look like you can simply add a folder you have to replace the existing connections dictionary with a replacement

ArcGISProject—ArcGIS Pro | Documentation

scroll wayyyyy down.  


... sort of retired...
0 Kudos