You can relate a feature in one table to a feature in another table for a variety of reasons, such as to allow your users to view information in one table for features in another. For example, you might want a user to be able to view fire hydrant inspections from an inspections table and view general fire hydrant information (such as install date) from a hydrants table — for the same set of fire hydrants.
When you relate features, you're editing them because the relate operation adds an attribute to the destination feature. The relate operation associates the two tables by adding a keyField attribute in the destination feature based on the corresponding origin feature. The keyField value is the origin table feature's primary key or global ID. In this hydrant example, relating a hydrant and an inspection adds the keyField value of the hydrant feature in the hydrants table (origin table) to the corresponding (hydrant) field in the inspection feature in the inspections table (destination table).
The pattern for editing a feature to relate it to another is the same as for editing a "regular" feature, as described in the Edit topic, whether the feature is the origin feature in a table relationship or the destination feature. When tables participate in a relationship like this, you can also perform the following editing-oriented operations:
Other considerations such as supported data sources are in the Additional information section below.
You use the relate method to relate a feature in one table to a feature in a different table as long as the two participating tables have a valid relationship between them. The relate method sets up the foreign key (
Key) value on the destination feature and can be called on either the origin or destination feature.
Relating features is akin to changing the attributes of feature; it's synchronous and done in memory. A feature once related must be added or updated to the table using
FeatureTable.update(_:) for changes to be committed.
It's important to know the cardinality of the table relationship when you relate features.
The sample feature service at http://sampleserver6.arcgisonline.com/arcgis/rest/services/ServiceRequest/FeatureServer contains a layer of point features that represent customer requests for service. A related table contains zero or more comments for each feature in the service request layer.
You may want to unrelate two already related features. For example, when a feature has a constraint violation, such as an invalid cardinality, you may want to undo the last relate operation that caused the violation. Unrelating features resets the
Key of the destination feature. Like the relate method, unrelate is also synchronous and is done in memory. Therefore
FeatureTable.update(_:) must be called for changes to be committed to the table.
You can check if a given feature violates any relationship constraints (for example, when inspections for a hydrant are being added by a user), such as cardinality violations. In 1:1 relationships, if an origin feature is already related to a destination feature, no other destination feature can be related to it. In 1:n relationships, a destination feature can be related to only one origin feature while an origin feature can be related to one or more destination features. For more info on cardinality and types of relationships, see ArcGIS Pro's help topic Relationship class properties or ArcMap's help topic Relationship class properties.
Considering the hydrants and inspection example above with a relationship cardinality of 1:1, if an inspection were to be related to a second hydrant, it would be a cardinality violation.
Note that an update operation on a feature succeeds even if a relationship constraint has been violated by the edit. This is consistent handling with ArcGIS Server and ArcGIS Desktop, which do not enforce cardinality violations. ArcGIS Desktop provides a Validate Feature tool, which allows you to recover from violations in a back-office operation after applying edits or syncing, if you choose to do so. For more information see ArcGIS Pro help's Relationship or ArcMap's Validating features and relationships. However, if you wish to check for violations in your app and recover from them, you can use the validate method.
Another type of constraint violation captured by the validate method occurs in a composite relationship when an orphan feature is added to the destination table without relating it to an origin feature. You can recover from this violation by relating the orphaned destination feature to a valid origin feature.
Getting related tables for a layer or table on the map returns only those tables on the map. Similarly, related queries require both origin and destination table/layer to be present on the map. For tables not on the map, you can query them using regular query operations but cannot use related queries. You can also view what relationships the tables participate in.
All the tables participating in a relationship must be present in the data source. Related tables are supported in the following data sources:
- ArcGIS feature service
- ArcGIS map service
- Geodatabase downloaded from a feature service
- Standalone mobile geodatabases authored in ArcGIS Pro
- Geodatabase in a mobile map package
Two participating tables can have one of the following cardinalities: one-to-one, one-to-many, or many-to-many.
When defining relationships, one table must be origin and the other destination. A table can participate in more than one relationship. A table can play a destination role in one relationship and origin in another, resulting in nested relationships.
Simple and composite workflow-based relationships are supported:
- In a simple relationship, features in the destination table are independent to features in the origin table. For example, a transformer and an electric pole may be related but they can also exist on their own. Deleting an origin feature resets the keyField of the relationship to NULL in the destination feature.
- In a composite relationship, each feature in the destination table is expected to be related to an origin feature. In other words, any orphan destination features are considered a violation of the relationship. For example, a hydrant and its inspection must be related. While the hydrant (origin) can exist on its own, an inspection (destination) must be related to a hydrant. When an origin feature is deleted, the destination feature should also be deleted. This is known as a cascade delete.