My own primary key generation

I need to set my own values for the primary keys in my objects/tables.

What is the best place to do this ?

I tried the write property of the object in the entities.pas unit.  But it didnt do anything.

Any suggestions ?

Hi Alan,

"I tried the write property of the object in the entities.pas unit.  But it didnt do anything."

what exactly you mean with that? It depends on what you want to achieve, there are a couple of ways to do so. The simplest one is just map your Id using TIdGenerator.None (which means Aurelius won't touch your id value). Then you just set the Id property yourself before saving.


Sorry I didn't explain it too well.

Its an existing or legacy database that currently uses our own code to set the primary key value in the database table.  It doesn't use SQL server auto sequencing.

I already set the TidGenerator to none.  That allows me do a POST from a REST client and insert the new row by setting the primary key field value in the json text manually.  That works fine.

But that means that the person using the POST has to know how to decide what value to make the primary key. I don't want that.  

I want to be able to execute my code that sets the primary key value on the REST server.  Not in the JSON on the client.

See what I mean ?

I experimented with the 'write property' of the primary key field, but couldnt get it to work.  Is that worth pursuing, or is that a better way ?  Like a BeforeAdd event for each object ?

Just before it calls the SQL to add the new row I want to be able to set the primary key.  Or at some point.  

You can create a new generator class and implement the code to get the id there. It's not documented yet because it's subject to change, but it's relatively easy. You can check, for example, how the class of the GUID generator is implemented (open unit Aurelius.Id.Guid.pas).

You can then change the code in GenerateId method by your own code. Then use the Id this way using Id attribute:

[Id('FID', TMyCustomGenerator)]

where TMyCustomerGenerator is the name of your class descending from TIdentifierGenerator

Thanks.  I can try that when we have the source.  We only have the trial version at present.  So I don't seem to have

But more generally, if I want to set the values of any of the fields in the DB table in my REST server, where is the best place to do this ?  For example if a REST client inserts a new row (object) I want to set some of the values of fields on the server, so that the RESTclient does not need to know about them.

I was expecting a 'beforeInsert' type of event that would allow for this.

Currently you can do that by inheriting TXDataServerModule and overriding some virtual methods to do that. In the very next version we will add an event system to make that process more straight forward.