I have some Python code that is launched from within an ArcMap project. Any joins that the user may have created in the project must be removed in order for my code to run. Unfortunately, the code that removes a join… arcpy.RemoveJoin_management("layer1", "layer2")… also breaks some of the layer properties that are critical to my application (highlighted fields, read-only fields, etc).

If joins are removed by right-clicking the layer in ArcMap and choosing “Remove Joins” the layer properties are left intact.

If I can detect that a join exists from within my code, I will simply exit the code and display a message that the user must manually remove their joins before attempting to run the code. So… Can a Join be detected programmatically?

  • I'm a bit lost on how doing the RemoveJoin via arcpy causes problems. How does it going about ruining a read-only field? Also, does using the Remove Join tool in ArcMap cause the same issues? – Nathanus Mar 24 '11 at 21:49
  • Maybe another way to tackle this would be to make your python code insensitive to joins? – Dan S. Mar 24 '11 at 23:17
  • @ Nathanus - The manual Remove Join in ArcMap does not break my layer properties, the GP tool does. Here is a relevant quote from ESRI Help: "As these tools perform the actual behind-the-scenes join processing slightly differently than the Join Data dialog box, use the tools if you encounter any unexpected issues with the join functionality on that dialog box." – BrianPeasley Mar 25 '11 at 0:37
  • @ Dan S. - I am using insert cursors and such in my code. I have no idea how I would go about making my code insensitive to joins. – BrianPeasley Mar 25 '11 at 0:40
  • 1
    well, it was worth asking. ;) forums.esri.com/Thread.asp?c=93&f=1729&t=293173 seems to imply that you may be able to update values in a joined table by prefixing column names by the table name, though; maybe it would work across an insert too? – Dan S. Mar 25 '11 at 1:18

Too bad there's not a hasJoin property on the arcpy.Layer class. I think you can test for a join by looking at field names though. Here's a simple proof of concept for data in a file geodatabase:

import arcpy, arcpy.mapping as arc

def joinCheck(lyr):
  fList = arcpy.Describe(lyr).fields
  for f in fList:
    if f.name.find(lyr.datasetName) > -1:
      return True
  return False

arcpy.env.workspace = r'<path_to_your_gdb>'
mxd = arc.MapDocument(r'<path_to_your_mxd>')
lyrs = arc.ListLayers(mxd)
for lyr in lyrs:
  # ignore group layers
  if not lyr.isGroupLayer:
    hasJoin = joinCheck(lyr)
    if hasJoin:
      print '\nFound a join: %s.' % lyr.datasetName
      print '\nNo join found on %s.' % lyr.datasetName
| improve this answer | |
  • This looks promising! I don't understand the meat of what it is doing: "if f.name.find(lyr.datasetName) > -1:" but I will try it in a few days and report back (I'm about to get snowed in & lose power!)... Thanks! – BrianPeasley Mar 25 '11 at 0:55
  • Glad to help. Check out the built in find method for python strings: docs.python.org/library/string.html and also the doc for arcpy field objects: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Field/… I'd say give this code a shot with your data and see if it works. – Derek Swingley Mar 25 '11 at 4:25
  • 2
    I ended up using a function very similar to this one and it works great... For each layer in my document, I loop through the fields list and use Python to determine whether there is a "dot" in the field name. – BrianPeasley Mar 29 '11 at 2:26
  • The "dot" in the field name approach is a choice with arcpy.mp (pro) module as there is no datasetName property available for Layer – Aamir Suleman May 2 at 5:29

I think you will find that there is no bulletproof way to do this with GP objects, you'll need to use ArcObjects and comtypes. Here's some discussion from the ESRI forums on the difficulties in checking for joins with the standard GP tools/objects: https://geonet.esri.com/thread/20317

| improve this answer | |
  • ArcObjects and comtypes aren't going to be the solution for this project, they are foreign to me and this project is due yesterday. Thanks for pointing me to that forum thread! I'm going to try the idea presented there: "...check whether any of the fieldname.split(".) would result in 2 parts of which the first would be the Join table!" – BrianPeasley Mar 25 '11 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.