Add Attribute Rule (Data Management)

Summary

Adds an attribute rule to a dataset.

Attribute rules are user-defined rules that can be added to a dataset to enhance the editing experience and help enforce data integrity. These rules can be used to populate attribute values or constrain permissible feature configurations and are enforced during feature editing. If a rule is violated when editing a feature, an error message will be returned.

Learn more about attribute rules

Learn about the Arcade scripting language

Usage

  • Batch calculation and validation rules in an enterprise geodatabase are evaluated using the validation server for datasets that are branch versioned. To add these rule types, the database connection must have Versioning Type set to Branch.

  • The dataset must have a GlobalID field.

  • To add batch calculation or validation rules to a dataset, editor tracking must be enabled.

  • Adding an attribute rule requires an exclusive lock on the dataset. You must close any active connections to the dataset, which may include stopping services.

  • The expression used to define an attribute rule is limited to the functionality available using the Arcade language.

  • You can reference a database sequence created by the Create Database Sequence tool in the Script Expression parameter using the NextSequenceValue Arcade data function.

  • To transfer existing attribute rules to another dataset, you can use the Export Attribute Rules and Import Attribute Rules tools instead of running this tool multiple times.

  • Note:

    Once you add attribute rules to a dataset, the minimum client version for the dataset is ArcGIS Pro 2.1. This means that the dataset will no longer be available for use in ArcMap.

Syntax

arcpy.management.AddAttributeRule(in_table, name, type, script_expression, {is_editable}, {triggering_events}, {error_number}, {error_message}, {description}, {subtype}, {field}, {exclude_from_client_evaluation}, {batch}, {severity}, {tags})
ParameterExplanationData Type
in_table

The table or feature class that will have the new rule applied.

Table View
name

A unique name for the new rule.

String
type

Specifies the type of attribute rule to add.

  • CALCULATIONAutomatically populate attribute values for features when another attribute is set on a feature. These rules are applied based on the triggering events specified. Long running calculations can be set to run in batch mode and will be evaluated at a user-defined time.When adding multiple calculation rules, the order in which the rules are added is important if there are circular dependencies. For example, Rule A calculates Field1 is equal to the value of $feature.Field2 + $feature.Field3, and Rule B calculates Field4 is equal to $feature.Field1 + $feature.Field5; the results of the calculation may be different depending on the order in which the rules are added.
  • CONSTRAINTSpecify permissible attribute configurations on a feature. When the constraint rule is violated, an error is generated and the feature is not stored. For example, if the value of Field A must be less than the sum of Field B and Field C, an error will be generated when that constraint is violated.
  • VALIDATIONCheck for existing features with a batch validation process. Rules are evaluated at a user-defined time. When a rule is violated, an error feature is created. The type of rule can only be used for data that has been set up for branch versioning.
String
script_expression

The Arcade expression that defines the rule.

Calculator Expression
is_editable
(Optional)

Specifies whether the attribute value can be edited. Attribute rules can be configured to either block or allow editors to edit the attribute values of the field being calculated. This parameter is only applicable for the calculation attribute rule type.

  • EDITABLEEditors will be able to edit the attribute value. This is the default.
  • NONEDITABLEEditors will not be able to edit the attribute value.
Boolean
triggering_events
[triggering_events,...]
(Optional)

Specifies the editing events that will trigger the attribute rule to take effect. This parameter is valid for calculation and constraint rule types only. At least one triggering event must be provided for calculation rules in which the Batch parameter is unchecked (batch = "NOT_BATCH" in Python). Triggering events are not applicable for calculation rules that have the Batch parameter checked (batch = "BATCH" in Python).

  • INSERTThe rule will be applied when a new feature is added.
  • UPDATEThe rule will be applied when a feature is updated.
  • DELETEThe rule will be applied when a feature is deleted.
String
error_number
(Optional)

An error number that will be returned when this rule is violated. This value is not required to be unique, so you may have the same custom error number returned for multiple rules.

This parameter is required for the constraint and validation rules. It is optional for calculation rules.

String
error_message
(Optional)

An error message that will be returned when this rule is violated. It is recommended that you use a descriptive message to help the editor understand the violation when it occurs. The message is limited to 2000 characters.

This parameter is required for the constraint and validation rules. It is optional for calculation rules.

String
description
(Optional)

The description of the new attribute rule. The description is limited to 256 characters.

String
subtype
(Optional)

The subtype to which the rule will be applied if the dataset has subtypes.

String
field
(Optional)

The name of an existing field to which the rule will be applied. This parameter is only applicable for the calculation attribute rule type.

String
exclude_from_client_evaluation
(Optional)

Specifies whether the rule will be excluded from evaluation before edits are applied. Because not all clients may have the capability to run all of the available rules, you can choose to flag a rule for simple clients only. For example, some rules may refer to data that has not been made available to all clients (reasons can include the data is offline, size, or security), or some rules may depend on the user or context (that is, a lightweight field update in ArcGIS Collector may not execute some rule that requires additional user input or knowledge; however, a client such as ArcGIS Pro may support it). This parameter is not applicable for validation rules or calculation rules if the batch parameter value is BATCH.

  • EXCLUDEThe rule will be excluded from client evaluation.
  • INCLUDEThe rule will be executed for all clients. This is the default.
Boolean
batch
(Optional)

Specifies whether the rule evaluation will be executed in batch mode.

  • BATCHThe rule evaluation will be executed in batch mode at a later time by executing validate.
  • NOT_BATCHThe rule evaluation will not be executed in batch mode. Triggering events will be used to determine when the rule is evaluated for insert, update, or delete operations. This is the default.

Calculation rules can be either BATCH or NOT_BATCH. Validation rules are always BATCH for this parameter, and constraint rules are always NOT_BATCH.

Batch rules are only supported for data with branch versioning enabled.

Boolean
severity
(Optional)

The severity of the error.

A value within the range of 1 through 5 can be chosen to define the severity of the rule. A value of 1 is high, being the most severe, and a value of 5 is low, being the least severe. For example, you can provide a low severity for a specific attribute rule and ignore the error during data production workflows, or set a high severity in which the error would need to be fixed for accuracy of data collected.

This parameter is only applicable to validation rules.

Long
tags
[tags,...]
(Optional)

A set of tags that identify the rule (searchable and indexable) as a way to map to a functional requirement in a data model. To enter multiple tags, use a semicolon delimiter, for example, Tag1;Tag2;Tag3.

String

Derived Output

NameExplanationData Type
out_table

The updated input table with an attribute rule added.

Table View

Code sample

AddAttributeRule example 1 (Python window)

Add a calculation rule that calculates the labeltext field of the GasPipes feature class.

'''****************************************************************************
Name:        AddAttributeRule_example1.py
Description: This script adds a calcualtion rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "calculateRuleLabel"
script_expression = 'if ($feature.material == 5) {return "Plastic"} else {return "Other"}'
triggering_events = "INSERT;UPDATE"
description = "Populate label text"
subtype = "Coated Steel"
field = "labeltext"

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, "CALCULATION", script_expression, "EDITABLE", triggering_events, "", "", description, subtype, field)
AddAttributeRule example 2 (Python window)

Add a constraint rule that limits the operating pressure attribute of the GasPipes feature class to 300.

'''****************************************************************************
Name:        AddAttributeRule_example2.py
Description: This script adds a constraint rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "constraintRuleOP"
script_expression = '$feature.OPERATINGPRESSURE < 300'
triggering_events = "INSERT;UPDATE"
description = "Operating pressure must be less than 300"
subtype = "ALL"
error_number = 2001
error_message = "Invalid operating pressure. Must be less than 300."

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, "CONSTRAINT", script_expression, "EDITABLE", triggering_events, error_number, error_message, description, subtype)
AddAttributeRule example 3 (Python window)

Add a validation rule to the GasPipes feature class to flag any existing features with an operating pressure greater than 300.

'''****************************************************************************
Name:        AddAttributeRule_example3.py
Description: This script adds a validation rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "validationRuleMaxOP"
rule_type = "VALIDATION"
script_expression = "$feature.OPERATINGPRESSURE < 300"
error_number = 3001
error_message = "Maximum operating pressure exceeded"
description = "Validation rule: max operating pressure value"
subtype = "Coated Steel"
batch = "BATCH"
severity = 3
tags = "OP;MAXOP"

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, rule_type, 
                                  script_expression, "", "", 
                                  error_number, error_message, 
                                  description, subtype, "", "", 
                                  batch, severity, tags)

Environments

Licensing information

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

Related topics