Question
· 22 hr ago

What is the proper way to get a new ID for a new record for a self-maintained primary key aka idkey?

Hi Developers!

Sometimes we need to deal with classes/tables where the primary key and the IdKey are something that is maintained by yourself.

What is the proper way to generate a new ID in case where ID is a %BigInt?

Property id As %Library.BigInt

Are there any system methods to provide it?

There is data already imported via SQL, so there is no last ID stored in ^myclassD, so I cannot do $I(^myclassD).

Thinking of:

set newid=$O(^myclassD(""),-1),newid=$I(newid)

What do you think?

Product version: IRIS 2025.3
Discussion (8)3
Log in or sign up to continue

I'm surprised that   data is already imported via SQL but ^myclassD has no content.
So take a look into the related generated class:

Storage Default
{
<Data name="DirDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
- - - - - -
after all properties
</Data>
<DataLocation>^oex.DirD</DataLocation>
<DefaultData>DirDefaultData</DefaultData>
<ExtentSize>986</ExtentSize>
<IdLocation>^oex.DirD</IdLocation>
<IndexLocation>^oex.DirI</IndexLocation>

 <IdLocation> is the one to go for
with all the new Storage strategies (columnar, sharded, ...  )
This might be quite an exotic global name and rarely the traditional.

Id location is in place - and has Ids in the first index, like ^myclassD(1),^myclassD(2), etc.

But nothing is set in ^myclassD

Other parameters I have:

Property id As %Library.BigInt [ SqlColumnNumber = 14 ];

Parameter ALLOWIDENTITYINSERT = 1;

Index MYCLASSPKEY1 On id [ IdKey, PrimaryKey, SqlName = MYCLASSPKEY1, Unique ];

And I import IDs from the csv file.

Since you manage the ID yourself, in the general case it may not necessarily be a simple counter, but, for example, a Fibonacci sequence.
If the source code of the class or table is available, it is better to see how the ID is generated in it. If there is no source code, and you are sure that this is a simple counter, then in my opinion it would be easier to make a standard ID and let IRIS manage it itself.

You can get the required Global reference like this programmatically:

 ; get compiled class  with your classname
USER>set classname="oex.Dir"
USER>set dic=##class(%Dictionary.CompiledClass).%OpenId(classname)
 ; get relationship to CompiledStorage
USER>set stor=dic.Storages.GetAt(1)
 ; get name of the ID-Global
USER>Write stor.IdLocation
^oex.DirD
USER>