Unique Constraints in R-COM Gateway
A Unique Constraint is a validation rule that prevents duplicate records from being created based on one or more selected attributes.
In an Object Schema, each Object represents a record. A unique constraint defines which attribute value, or combination of attribute values, must be unique across all Objects in that group. This helps maintain clean and reliable data by ensuring that the same entity is not added multiple times.
For example, if an Object Schema stores employee records, the employee_id attribute can be configured as a unique constraint. This means no two employee records can have the same employee ID.
Why Unique Constraints Are Used
Unique constraints are used to prevent duplicate data and enforce business rules.
They are useful when certain values must identify a record uniquely, such as:
| Example Attribute | Reason for Uniqueness |
|---|---|
employee_id | Each employee should have only one employee ID. |
serial_number | Each device should have a unique serial number. |
product_code | Each product should have one unique product code. |
asset_tag | Each tracked asset should have a unique asset tag. |
Without unique constraints, users or workflows may accidentally create duplicate Objects with the same identifying information. This can cause incorrect reports, duplicate workflow actions, tracking errors, or inconsistent data.
Single-Attribute Unique Constraint
A single-attribute unique constraint checks one attribute.
Example
| Constraint | Rule |
|---|---|
serial_number | No two Objects can have the same serial number. |
Existing Object
{
"serial_number": "SN-10001",
"name": "Scanner A"
}
New Object
{
"serial_number": "SN-10001",
"name": "Scanner B"
}
Result
- Rejected
Reason
- The
serial_numbervalue already exists.
Multi-Attribute Unique Constraint
A unique constraint can also use multiple attributes. In this case, the combination of values must be unique.
Example
| Constraint | Rule |
|---|---|
name + DOB | No two Objects can have the same name and date of birth combination. |
Existing Object
{
"name": "John Smith",
"DOB": "1990-04-15"
}
New Object
{
"name": "John Smith",
"DOB": "1990-04-15"
}
Result
- Rejected
Reason
- The same combination of
nameandDOBalready exists.
However, the following Object would be accepted:
{
"name": "John Smith",
"DOB": "1988-09-10"
}
Reason
- The complete combination is different.
How Unique Constraint Validation Works
When a new Object is added to an Object Schema, R-COM Gateway checks it against all configured unique constraints.
The Object is created only if all unique constraints pass.
If any constraint fails, the Object is not created because it would violate a uniqueness rule.
For a constraint to be valid during validation, at least one attribute in that constraint must contain a value. If all attributes in a constraint are empty, the constraint cannot pass.
Example
Consider an Object Schema used to store medical equipment.
Configured unique constraints:
| Constraint | Purpose |
|---|---|
equipment_id | Ensures each equipment has a unique internal ID. |
serial_number | Ensures each physical device is registered only once. |
name + location | Prevents duplicate equipment records with the same name in the same location. |
When a user adds a new equipment Object, the system checks all three constraints.
- If the
equipment_idalready exists, the Object is rejected. - If the
serial_numberalready exists, the Object is rejected. - If the
nameandlocationcombination already exists, the Object is rejected.
Summary
A Unique Constraint ensures that specific attribute values, or combinations of values, remain unique within an Object Schema. It helps prevent duplicate Objects, improves data quality, and enforces important business rules.
Use unique constraints for attributes that identify an Object, such as:
employee_idserial_numberproduct_codeasset_tag- Other values that should not be repeated