The Object ID
The Object ID
When you save an object for the first time, the system creates two permanent identifiers for it: the ID (discussed here) and the OID (discussed in the next section). You can use these identifiers later to access or remove the saved objects.
The object ID is the more commonly used identifier, and you use this ID as the first argument when you call methods such as %OpenId(), %ExistsId(), and %DeleteId(), available in all persistent classes.
Determining the ID
The system can automatically generate an integer to use as the ID. However, it is often useful to have an ID that is more meaningful to your application, so a common practice is to add an IdKey index definition to the persistent class, where this index refers to the property or properties that will provide the ID value. The following example includes an IdKey index that refers to the SSN property:
Class MyApp.Person Extends %Persistent
{
Index MainIDX On SSN [ Idkey ];
Property SSN As %String;
// other properties
}
In this example, the SSN for a person serves as the ID (and can be used with methods such as %OpenId(), %ExistsId(), and %DeleteId()). For example:
set person=##class(MyApp.Person).%OpenId("123-45-6789")
When you add an IdKey index to a class, the compiler generates additional methods (known as index methods) for that class, which you can use to access or remove objects. The index methods include indexNameOpen(), indexNameExists(), and indexNameDelete(), where indexName is the name of the IdKey index. When the IdKey index refers to only a single property, these methods accept the value of that property as the first argument. For example:
set person=##class(MyApp.Person).MainIDXOpen("123-45-6789")
You can have an IdKey index that refers to multiple properties, as follows:
Class MyApp.Account Extends %Persistent
{
Index MainIDX On (CountryCode,RegionalID) [ Idkey ];
Property CountryCode As %String;
Property RegionalID As %String;
// other properties
}
For such a class, the ID has a more complex form; it consists of the property values used by the IdKey index, concatenated with pairs of pipe || characters. In this case, it is simpler to use the generated index methods. The expected arguments are the property values used by the IdKey index, in the order used by that index. For example:
set account=##class(MyApp.Account).MainIDXOpen("US","1234567")
Projection of Object IDs to SQL
The ID of an object is available in the corresponding SQL table. If possible, InterSystems IRIS uses the field name ID. InterSystems IRIS also provides a way to access the ID if you are not sure what field name to use. The system is as follows:
-
An object ID is not a property of the object and is treated differently from the properties.
-
If the class does not contain a property named ID (in any case variation), then the table also contains the field ID, and that field contains the object ID. For an example, see the previous section.
-
If the class contains a property that is projected to SQL with the name ID (in any case variation), then the table also contains the field ID1, and this field holds the value of the object ID.
Similarly, if the class contains properties that are projected as ID and ID1, then the table also contains the field ID2, and this field holds the value of the object ID.
-
In all cases, the table also provides the pseudo-field %ID, which also holds the value of the object ID.
Object IDs in SQL
InterSystems IRIS enforces uniqueness for the ID field (whatever its actual name might be). InterSystems IRIS also prevents this field from being changed. This means that you cannot perform SQL UPDATE or INSERT operations on this field. For instance, the following shows the SQL needed to add a new record to a table:
INSERT INTO PERSON (FNAME, LNAME) VALUES (:fname, :lname)
Notice that this SQL does not refer to the ID field. InterSystems IRIS generates a value for the ID field and inserts that when it creates the requested record.