Unique and Constraints

Developers can define different kinds on constraints on model attributes. The general grammar of constraints are defined in the TQL Model grammar, but they can be used across TQL, such as in queries.

Unique

The Unique constraint declared for a model attribute ensures that the attributes values are unique across all instances of that model. The grammar structure of Unique contains a name and a value. For example <Unique Name="UniqueVendorName" Value="VendorName"/>.

The Name is a developer chosen name. The Value is the name of the attribute this Unique will apply to.

In this example Unique is declared for the attribute VendorName.

Unique Constraint on Attribute LightNumber
#
DataModel(name: "VendorInfo"): 
  Sid(name: "VendorSysId")
  String(name: "VendorName")
  String(name: "VendorTitle")
  PostalAddress(name: "VendorAddress")
  String(name: "DeviceTypes", Cardinality: "0..m")
  Unique(name: "UniqueVendorName", Value: "VendorName")

If you try to create more than one VendorInfo with the same VendorName for the model, the Create TQL Query will fail with Unique constraint violation

Unique Constraint Violation Error
#
Create(Status: "Failure"): 
  VendorInfo: 
    VendorSysId: chag3vw7tcet7dvg2athubjwl2gvth73/VendorSysId: 
    VendorName(Status: "Failure!UpdateConflict:Create:Record exists:integrity constraint violation: unique constraint or index violation; SYS_PK_10098 table: TR;", Value: "Phidget")
    VendorTitle(Status: "Failure!UpdateConflict:Create:Record exists:integrity constraint violation: unique constraint or index violation; SYS_PK_10098 table: TR;", Value: "Sensor Provider")
    VendorAddress: 
      Street(Status: "Failure!UpdateConflict:Create:Record exists:integrity constraint violation: unique constraint or index violation; SYS_PK_10098 table: TR;", Value: "123 TQL Way")
      City(Status: "Failure!UpdateConflict:Create:Record exists:integrity constraint violation: unique constraint or index violation; SYS_PK_10098 table: TR;", Value: "Sunnyvale")
      Zipcode(Status: "Failure!UpdateConflict:Create:Record exists:integrity constraint violation: unique constraint or index violation; SYS_PK_10098 table: TR;", Value: "91234")

Inline Constraint

Inline Constraint is a constraint written as an XML attribute, and directly apply to the element to which it belongs. Inline Constraint can be of any subtype of Constraint.

Subtypes of the type Constraint include:

Eq, Le, Lt, Ge, Gt, Ne, In, Like, Between, Not, And, Xor, Or.

See System Defined Types

For example:

Inline Constraint example
#
Def(name: "Manager", inherits: "Employee"): 
     String(name: "Employment.BusinnesRole", default: "manager", eq: "manager", cardinality: "1", modifiers: "hidden,readonly")

Standalone Constraint

Constraint can be defines a block under model definition.

Inline Constraint example
#
Def(name: "Janitor", inherits: "Employee"): 
      Constraint(target: "Employment.BusinnesRole", eq: "janitor")

Nested Constraint

Constraints can be nested by using And, Or and XoR constructs.

Inline Constraint example
#
DataModel(name: "OlegOrder"):
      Attribute(name: "Owner", value: "Order.Owner"):
        And:
          OR:
            EQ value: "Oleg")
            EQ value: "Michael")
          OR:
            EQ target: "ShippingCost", value: "10.2")
            EQ target: "TotalCost", value: "20.2")
      Attribute name: "ShippingCost", value: "Order.ShippingCost")
      Attribute name: "TotalCost", value: "Order.TotalCost")