Select to view content in your preferred language

SDE version editing with arcpy

7451
8
12-05-2013 02:01 PM
SarahDoyle
Emerging Contributor
Hi all,

I need to set up a way to edit a table and a feature class in versions of an SDE database.  The version is a parameter in a tool.  I must be missing a step, because I am not getting the Change Version function to work for any of my registered tables or the feature class.  Not even by converting the table to table view or the feature class to a feature layer.  The problem may be with the path.  Here is what I think should work:

out_gdb=r"T:\GIS\tools\SDE_Connection\CO_Stip_User.sde"

abstractTbl=os.path.join(out_gdb,"ilmco.ILMCODBO.Abstract")
arcpy.MakeTableView_management (plssTbl, "plss_2")
arcpy.ChangeVersion_management("plss_2", "TRANSACTIONAL", version_name)

minerals=os.path.join(out_gdb,"ilmco.ILMCODBO.Minerals")
arcpy.MakeFeatureLayer_management(minerals,"minerals_2")
arcpy.ChangeVersion_management("minerals_2", "TRANSACTIONAL", version_name)

The user versions are accessed through CO_Stip_User.sde.  The original database name is ilmco.  I've tried all sorts of combinations, but nothing is working!  Any ideas?

Thanks,
Sarah
0 Kudos
8 Replies
WilliamCraft
MVP Regular Contributor
Right, I'm not sure that you can change versions ON the actual table view and feature layer.  From the looks of your code, why not set the version when making the initial connection?  In other words, as an alternative you may be able to edit the .SDE connection file using arcpy, specifically the Transactional Version part of the connection side.  If the Transactional Version is a variable, you could use that variable in a For or While loop so that you can iterate through all of the versions you care about when making the edits you need.
0 Kudos
SarahDoyle
Emerging Contributor
Thanks, I guess I'm not sure about how to connect to the Transactional version (its not really a path).  Is this what you had in mind?

out_gdb=r"T:\GIS\tools\SDE_Connection\CO_Stip_User.sde"
versions = arcpy.ListVersions(out_gdb)
      for orig_version in versions:
             while orig_version == version:
                  minerals=os.path.join(out_gdb,"ilmcostip.ILMCODBO.Minerals")
            
Then whatever edits I make to minerals will only be in that version?
0 Kudos
WilliamCraft
MVP Regular Contributor
Yes, that is basically what I was referring to.  But since you would be looping through each version, the edits would be performed in all versions that you're looping through not just one.  Is that not your goal based on what you wrote?  Another possibility is to forget changing the version and instead simply deleting and re-creating the SDE connection file with the version you want as a parameter.  You can do that in arcpy too.  Either way, you will need to loop through your versions to make the same edits in all of them.
0 Kudos
SarahDoyle
Emerging Contributor
I don't want to edit every version, just the one chosen by a user as a tool parameter. 

I didn't know about arcpy.CreateArcSDEConnectionFile_management.  I can only find references about it for 10.0.  If that works in 10.1, that might be the best way to go.  Thanks for your help.
0 Kudos
SarahDoyle
Emerging Contributor
Hmmm, after creating the SDE connection, all of my edits are going to the default feature class and not the version.  Do I still have to change versions for each feature class/table?
0 Kudos
WilliamCraft
MVP Regular Contributor
If you set the version in the newly-created SDE connection file, all of the tables and feature classes you edit using that connection file should honor the specified version.  I'm not sure why it would edit the DEFAULT version if you're providing a valid version name.  It could be a bug at 10.0 but I'm not sure.  Do some searching online for any related bugs that might already be know.  Otherwise, I'd recommend opening a support ticket with Esri.  Sorry I couldn't help!
0 Kudos
VinceAngelo
Esri Esteemed Contributor
Sarah --

Your descriptions of what you're attempting don't match the vocabulary of Esri geodatabases,
e.g.,  "all of my edits are going to the default feature class and not the version".  There isn't a
"default feature class" (you always have to specify one), and all versioned edits have to take
place in *some* version.

It might help to go back and brush up on the versioning documentation. You may also want to
review Derek's Versioning 101.

When it comes down to simplest representation, a version is just the name of a state in the
state tree.  The state tree spans all tables within the geodatabase as one entity. Some states
could span multiple tables.   It's a complex implementation, but the end-user experience has
only one moving part -- the version.

Each connection allows you to determine what your base version is, and each edit session
allows you to change the active version.  Changes made to a version advance the state
and label it with the target version.  Further edits update the target version's state,
leaving behind intermediate states, which can be compressed, and potentially conflicting
state changes need to be reconciled.

For your task, I believe you need only identify the table to updated, the base version from
which you want to edit (the default is SDE.DEFAULT), and the name of the version which
will label your completed edits.  Scripting this in Python probably shouldn't be your initial
attempt at the process.  Once you get comfortable with the use cases you'll probably find
the process more intuitive.

- V
0 Kudos
SarahDoyle
Emerging Contributor
All,

Thanks for your help, but I figured out my issue.  There is a bug in 10.1 changing versions for table views.  Creating a new connection file is the only way to change the version in python.  It is fixed in 10.2.  Here is a link to the thread talking about the bug:
http://forums.arcgis.com/threads/54905-Changing-Versions-with-Python
0 Kudos