Create Relationship Class (Data Management)
Summary
This tool creates a relationship class to store an association between fields or features in the origin table and the destination table.
Usage
-
Relationships can exist between spatial objects (features in feature classes), nonspatial objects (rows in a table), or spatial and nonspatial objects.
-
Once created, a relationship class cannot be modified; you can only add, delete, or refine its rules. Relationship classes can be deleted and renamed using ArcCatalog in the same manner as any other object in the database.
-
For many-to-many relationship classes, a new table is created in the database to store the foreign keys used to link the origin and destination classes. This table can also have other fields to store attributes of the relationship itself that are not attributed to either the origin or destination class. For example, in a parcel database, you might have a relationship class between parcels and owners in which owners "own" parcels and parcels are "owned by" owners. An attribute of that relationship could be percentage ownership. One-to-one and one-to-many relationship classes can also have attributes; in this case, a table is created to store the relationships.
-
Simple or peer-to-peer relationships involve two or more objects in the database that exist independently of each other. For example, in a railroad network, you might have railroad crossings that have one or more related signal lamps. However, a railroad crossing can exist without a signal lamp, and signal lamps exist on the railroad network where there are no railroad crossings. Simple relationships can have one-to-one, one-to-many, or many-to-many cardinality.
-
A composite relationship is one in which the lifetime of one object controls the lifetime of its related objects. For example, power poles support transformers, and transformers are mounted on poles. Once a pole is deleted, a delete message is propagated to its related transformers, which are deleted from the transformers' feature class. Composite relationships are always one-to-many.
-
Forward and backward path labels describe the relationship when navigating from one object to another. The forward path label describes the relationship navigated from the origin class to the destination class. In the pole-transformer example, a forward path label might be "Poles support transformers." The backward path label describes the relationship navigated from the destination to the origin class. In the pole-transformer example, a backward path label might be"Transformers are mounted on poles."
-
Relationship classes can also be created in ArcCatalog. Select the command New > Relationship Class from the context menu of a geodatabase.
Syntax
Parameter | Explanation | Data Type |
origin_table |
The table or feature class that is associated to the destination table. | Table View |
destination_table |
The table that is associated to the origin table. | Table View |
out_relationship_class |
The relationship class that is created. | Relationship Class |
relationship_type |
The type of relationship to create between the origin and destination tables.
| String |
forward_label |
A name to uniquely identify the relationship when navigating from the origin table to the destination table. | String |
backward_label |
A name to uniquely identify the relationship when navigating from the destination table to the origin table. | String |
message_direction |
The direction in which messages are passed between the origin and destination tables. For example, in a relationship between poles and transformers, when the pole is deleted, it sends a message to its related transformer objects informing them it was deleted.
| String |
cardinality |
Determines how many relationships exist between rows or features in the origin and rows or features in the destination table.
| String |
attributed |
Specifies if the relationship will have attributes.
| Boolean |
origin_primary_key |
The field in the origin table, typically the OID field, that links it to the Origin Foreign Key field in the relationship class table. | String |
origin_foreign_key |
The field in the relationship class table that links it to the Origin Primary Key field in the origin table. | String |
destination_primary_key (Optional) |
The field in the destination table, typically the OID field, that links it to the Destination Foreign Key field in the relationship class table. | String |
destination_foreign_key (Optional) |
The field in the relationship class table that links it to the Destination Primary Key field in the destination table. | String |
Code Sample
The following Python Window script demonstrates how to use the Create Relationship Class tool.
import arcpy arcpy.env.workspace = "C:/data/Habitat_Analysis.gdb" arcpy.CreateRelationshipClass_management("vegtype", "vegtable", "veg_RelClass", "SIMPLE", "Attributes from vegtable", "Attributes and Features from vegtype", "NONE", "ONE_TO_ONE", "NONE", "HOLLAND95", "HOLLAND95")
Create a relationship class between vegetation feature class and table with additional vegetation information.
# Name: CreateRelationshipClass.py # Description: Create a relationship class between vegetation feature # class and table with additional vegetation information # Author: ESRI # import system modules import arcpy from arcpy import env # Set environment settings env.workspace = "C:/data" # Copy vegtable.dbf to file gdb table, since both tables to be related # must be in the same database vegDbf = "vegtable.dbf" vegTbl = "Habitat_Analysis.gdb/vegtable" arcpy.CopyRows_management(vegDbf, vegTbl) # Create simple relationship class between 'vegtype' vegetation layer # and 'vegtable' table with additional vegetation information veg = "Habitat_Analysis.gdb/vegtype" relClass = "Habitat_Analysis.gdb/veg_RelClass" forLabel = "Attributes from vegtable" backLabel = "Attributes and Features from vegtype" primaryKey = "HOLLAND95" foreignKey = "HOLLAND95" arcpy.CreateRelationshipClass_management(veg, vegTbl, relClass, "SIMPLE", forLabel, backLabel, "NONE", "ONE_TO_ONE", "NONE", primaryKey, foreignKey)
Create a relationship class between parcel feature class and a table with owner information.
# Name: CreateRelationshipClass.py # Description: Create a relationship class between parcels feature # class and table with owner information # Author: ESRI # import system modules import arcpy from arcpy import env # Set environment settings env.workspace = "C:/data" # Copy owners.dat to file gdb table, since both tables to be related # must be in the same database ownerDat = "owners.dat" ownerTbl = "Montgomery.gdb/owners" arcpy.CopyRows_management(ownerDat, ownerTbl) # Create simple relationship class between 'parcel' parcel layer # and 'owner' table with additional parcel owner information parcel = "Montgomery.gdb/Parcels" relClass = "Montgomery.gdb/parcelowners_RelClass" forLabel = "Owns" backLabel = "Is Owned By" primaryKey = "PROPERTY_ID" foreignKey = "PROPERTY_I" arcpy.CreateRelationshipClass_management(ownerTbl, parcel, relClass, "SIMPLE", forLabel, backLabel, "BACKWARD", "ONE_TO_MANY", "NONE", primaryKey, foreignKey)