Create Relationship Class (Data Management)

Summary

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.

  • 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 that are not attributed to either the origin class or the 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 might 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 can 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 class to the origin class. In the pole-transformer example, a backward path label might be Transformers are mounted on poles.

Parameters

LabelExplanationData Type
Origin Table

The table or feature class that is associated with the destination table.

Table View
Destination Table

The table that is associated with the origin table.

Table View
Output Relationship Class

The relationship class that will be created.

Relationship Class
Relationship Type

Specifies the type of relationship that will be created between the origin and destination tables.

  • SimpleThe origin and destination tables will have a simple relationship. This is the default.
  • CompositeThe origin and destination tables will have a composite relationship.
String
Forward Path Label

A name to uniquely identify the relationship when navigating from the origin table to the destination table.

String
Backward Path label

A name to uniquely identify the relationship when navigating from the destination table to the origin table.

String
Message Direction

Specifies the direction in which messages will be passed between the origin and destination tables. For example, in a relationship between poles and transformers, when a pole is deleted, a message will be sent to its related transformer objects indicating that it was deleted.

  • Forward (origin to destination)Messages will be passed from the origin table to the destination table.
  • Backward (destination to origin)Messages will be passed from the destination table to the origin table.
  • Both directionsMessages will be passed from the origin table to the destination table and from the destination table to the origin table.
  • None (no messages propagated)No messages will be passed. This is the default.
String
Cardinality

Specifies how many relationships will exist between rows or features in the origin table and rows or features in the destination table.

  • One to one (1:1)Each row or feature in the origin table can be related to zero or one row or feature in the destination table. This is the default.
  • One to many (1:M)Each row or feature in the origin table can be related to one or several rows or features in the destination table.
  • Many to many (M:N)Several rows or features in the origin table can be related to one or several rows or features in the destination table.
String
Relationship class is attributed

Specifies whether the relationship class will have attributes.

  • Checked—The relationship class will have attributes.
  • Unchecked—The relationship class will not have attributes. This is the default.
Boolean
Origin Primary Key

For many-to-many or attributed relationship classes, this is the field in the origin table that links to the Origin Foreign Key field in the relationship class table.

For one-to-one or one-to-many relationship classes that are not attributed, this is the field in the origin table that links to the Origin Foreign Key field in the destination table.

String
Origin Foreign Key

For many-to-many or attributed relationship classes, this is the field in the relationship class table that links to the Origin Primary Key field in the origin table.

For one-to-one or one-to-many relationship classes that are not attributed, this is the field in the destination table that links to the Origin Primary Key field in the origin table.

String
Destination Primary Key
(Optional)

The field in the destination table that links to the Destination Foreign Key field in the relationship class table. This value is required for many-to-many or attributed relationship classes, but should be left empty for one-to-one or one-to-many relationship classes that are not attributed.

String
Destination Foreign Key
(Optional)

The field in the relationship class table that links to the Destination Primary Key field in the destination table. This value is required for many-to-many or attributed relationship classes, but should be left empty for one-to-one or one-to-many relationship classes that are not attributed.

String

arcpy.management.CreateRelationshipClass(origin_table, destination_table, out_relationship_class, relationship_type, forward_label, backward_label, message_direction, cardinality, attributed, origin_primary_key, origin_foreign_key, {destination_primary_key}, {destination_foreign_key})
NameExplanationData Type
origin_table

The table or feature class that is associated with the destination table.

Table View
destination_table

The table that is associated with the origin table.

Table View
out_relationship_class

The relationship class that will be created.

Relationship Class
relationship_type

Specifies the type of relationship that will be created between the origin and destination tables.

  • SIMPLEThe origin and destination tables will have a simple relationship. This is the default.
  • COMPOSITEThe origin and destination tables will have a composite relationship.
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

Specifies the direction in which messages will be passed between the origin and destination tables. For example, in a relationship between poles and transformers, when a pole is deleted, a message will be sent to its related transformer objects indicating that it was deleted.

  • FORWARDMessages will be passed from the origin table to the destination table.
  • BACKWARDMessages will be passed from the destination table to the origin table.
  • BOTHMessages will be passed from the origin table to the destination table and from the destination table to the origin table.
  • NONENo messages will be passed. This is the default.
String
cardinality

Specifies how many relationships will exist between rows or features in the origin table and rows or features in the destination table.

  • ONE_TO_ONEEach row or feature in the origin table can be related to zero or one row or feature in the destination table. This is the default.
  • ONE_TO_MANYEach row or feature in the origin table can be related to one or several rows or features in the destination table.
  • MANY_TO_MANYSeveral rows or features in the origin table can be related to one or several rows or features in the destination table.
String
attributed

Specifies whether the relationship class will have attributes.

  • NONEThe relationship class will not have attributes. This is the default.
  • ATTRIBUTEDThe relationship class will have attributes.
Boolean
origin_primary_key

For many-to-many or attributed relationship classes, this is the field in the origin table that links to the origin_foreign_key field in the relationship class table.

For one-to-one or one-to-many relationship classes that are not attributed, this is the field in the origin table that links to the origin_foreign_key field in the destination table.

String
origin_foreign_key

For many-to-many or attributed relationship classes, this is the field in the relationship class table that links to the origin_primary_key field in the origin table.

For one-to-one or one-to-many relationship classes that are not attributed, this is the field in the destination table that links to the origin_primary_key field in the origin table.

String
destination_primary_key
(Optional)

The field in the destination table that links to the destination_foreign_key field in the relationship class table. This value is required for many-to-many or attributed relationship classes, but should be left empty for one-to-one or one-to-many relationship classes that are not attributed.

String
destination_foreign_key
(Optional)

The field in the relationship class table that links to the destination_primary_key field in the destination table. This value is required for many-to-many or attributed relationship classes, but should be left empty for one-to-one or one-to-many relationship classes that are not attributed.

String

Code sample

CreateRelationshipClass example (Python window)

The following Python window script demonstrates how to use the CreateRelationshipClass function.

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")
CreateRelationshipClass example 1 (stand-alone script)

Create a relationship class between a vegetation feature class and a 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)
CreateRelationshipClass example 2 (stand-alone script)

Create a relationship class between a 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_ID"
arcpy.CreateRelationshipClass_management(ownerTbl,
                                         parcel,
                                         relClass,
                                         "SIMPLE",
                                         forLabel,
                                         backLabel,
                                         "BACKWARD",
                                         "ONE_TO_MANY",
                                         "NONE",
                                         primaryKey,
                                         foreignKey)

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics