What do you do if you want to have the ID field have a meaningful name for your application?
Sometimes it comes to pass that when you're making a new table that you want to have the unique row identifier (a.k.a. IDKEY) to be a field that has a name that is meaningful for your data. Moreover, sometimes you want to set this value directly. Caché fully supports this functionality and it works Suppose you have a class Test.Kyle. The data will be stored like so:
^Test.Kyle(IDKEY)=$LB("",Field1,Field2,...,Fieldn)
Any other field can be unique, but the IDKEY is the one that is the subscript for all the data. There are two ways to create a named IDKEY.
- Property KyleID as %Integer [Identity];
- Property KyleID as %Integer;
Index IDKEYIndex on KyleID [IDKEY];
Number 1 works like the default IDKEY. It is not setable, and is automatically generated using $INCREMENT.
Number 2, on the other hand, is a property that you have to set manually (though you could give it an initial value). Moreover, the field being part of the IDKEY means it is inherantly required, though that keyword is never used.
Thanks Kyle!
Just for clarity, here's a little more context regarding the Identity keyword and the IdKey keyword. Specifically, regarding what other effects these might have on your table/class design.
Identity
IdKey
There are a couple of other options for this topic.
If you are simply wanting the ability to define the name of the "ID" field yourself, you can use the class keyword SqlRowidName. For example:
SqlRowIdName = PersonId
For an Identity property, there is a way to allow a process to explicitly specify values for the identity property/field. This is typically useful when first populating a table/object from an external source and you want to retain the identity's values.
The call is: set oldValue = $SYSTEM.SQL.SetIdentityInsert(1,.sc)
For more information on the $SYSTEM.SQL.SetIdentityInsert call see class documentation for the %SYSTEM.SQL class.
Here is an example: