Select to view content in your preferred language

Adding Relationship Class to Featue Service

1223
4
Jump to solution
10-19-2022 12:09 AM
MarcoPoetsch
Frequent Contributor

Hi there,

I would like to add a relationship class to one of my feature services.

There is already a GlobalID - ParentGLobalID relationship, and  want to have second one based on a String (Name - ParentName).

I tried using ArcGIS Pro to achieve this, however,I'm getting this very useless error message here:
https://pro.arcgis.com/en/pro-app/2.9/tool-reference/tool-errors-and-warnings/160001-170000/tool-err...

Is there a way to add this relationship via REST, or any other way?

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Frequent Contributor

Hi @MarcoPoetsch 

This can be achieved using the ArcGIS Python API similar to below.

Process:

  • Connect to AGOL
  • Access Feature Service
  • Get Layer and Layer ID
  • Get Table and Table ID (you can also relate to another Layer instead)
  • Create dictionary for Layer and Table relationships. (the example below shows a one-to-one, you can alter for alternate cardinality)
  • Update the Layer and Table definitions with the relationship

 

from arcgis import GIS

## connect to AGOL
agol = GIS("home")

## get the feature service using the item id
item = agol.content.get("FS_ITEM_ID")

## get the Feature Layer and its ID
## you might need to replace 0 with the proper layer index if more than one layer
lyr = item .layers[0]
lyr_id = lyr.properties.id

## get the Table and its ID
## you might need to replace 0 with the proper table index if more than one table
tbl = item.tables[0]
tbl_id = tbl.properties.id

## a dictionary containing the relationship properties for the Feature Layer
## REPLACE LAYER_FIELD with the name of your matching field in the layer
lyr_rel_dict = {
    "name": "Layer_to_Table",
    "relatedTableId": int(tbl_id),
    "cardinality": "esriRelCardinalityOneToOne",
    "role": "esriRelRoleOrigin",
    "keyField": "LYR_FIELD",
    "composite": True
}

## a dictionary containing the relationship properties for the Table
## REPLACE TABLE_FIELD with the name of you matching field in the table
tbl_rel_dict = {
    "name": "Table_to_Layer",
    "relatedTableId": int(lyr_id),
    "cardinality": "esriRelCardinalityOneToOne",
    "role": "esriRelRoleDestination",
    "keyField": "TABLE_FIELD",
    "composite": True
}

## update the relationship properties in the Feature Layer and Table
lyr.manager.add_to_definition({"relationships" : [lyr_rel_dict]})
tbl.manager.add_to_definition({"relationships" : [tbl_rel_dict]})

 

 

 

~ learn.finaldraftmapping.com

View solution in original post

4 Replies
Clubdebambos
Frequent Contributor

Hi @MarcoPoetsch 

This can be achieved using the ArcGIS Python API similar to below.

Process:

  • Connect to AGOL
  • Access Feature Service
  • Get Layer and Layer ID
  • Get Table and Table ID (you can also relate to another Layer instead)
  • Create dictionary for Layer and Table relationships. (the example below shows a one-to-one, you can alter for alternate cardinality)
  • Update the Layer and Table definitions with the relationship

 

from arcgis import GIS

## connect to AGOL
agol = GIS("home")

## get the feature service using the item id
item = agol.content.get("FS_ITEM_ID")

## get the Feature Layer and its ID
## you might need to replace 0 with the proper layer index if more than one layer
lyr = item .layers[0]
lyr_id = lyr.properties.id

## get the Table and its ID
## you might need to replace 0 with the proper table index if more than one table
tbl = item.tables[0]
tbl_id = tbl.properties.id

## a dictionary containing the relationship properties for the Feature Layer
## REPLACE LAYER_FIELD with the name of your matching field in the layer
lyr_rel_dict = {
    "name": "Layer_to_Table",
    "relatedTableId": int(tbl_id),
    "cardinality": "esriRelCardinalityOneToOne",
    "role": "esriRelRoleOrigin",
    "keyField": "LYR_FIELD",
    "composite": True
}

## a dictionary containing the relationship properties for the Table
## REPLACE TABLE_FIELD with the name of you matching field in the table
tbl_rel_dict = {
    "name": "Table_to_Layer",
    "relatedTableId": int(lyr_id),
    "cardinality": "esriRelCardinalityOneToOne",
    "role": "esriRelRoleDestination",
    "keyField": "TABLE_FIELD",
    "composite": True
}

## update the relationship properties in the Feature Layer and Table
lyr.manager.add_to_definition({"relationships" : [lyr_rel_dict]})
tbl.manager.add_to_definition({"relationships" : [tbl_rel_dict]})

 

 

 

~ learn.finaldraftmapping.com
MarcoPoetsch
Frequent Contributor

Sweet. Thanks @Clubdebambos 

May I ask you how I would DELETE a relationship class? 🙂

Clubdebambos
Frequent Contributor

Hi @MarcoPoetsch 

I recommend asking this a separate question so it has its own answer and for others to find the answer to it the future.

 

~ learn.finaldraftmapping.com
0 Kudos