How to Update Primary key?

this code working well

 Manager := TObjectManager.Create(MyConnection);
 Person := Manager.Find<TPerson>(PersonId);
 Person.name := 'xxxxxxxx';
 Manager.Flush;
 Manager.Free;

but when cosider to change the primay key of current person
 Person.PersonId := 1000;
it doesn't work because it generate sql 
"update person set 
  code = :A_code, 
  name = :A_name, 
WHERE code = :p_4;

A_code = "1000" (ftString)
A_name = "xxxxxx" (ftString)
p_4 = "1000" (ftString)
"
the Old value of code is the same as the new one, So how to update the primary key?

Yes, you can't do that. If you change the object id, Aurelius will think it's a different object. You will have to execute an SQL manually to do that.

Hello Wagner,

I would like to know if there have been any updates in TMS Aurelius that allow defining the primary key of an object before saving it.

Here is an example of my model:

[Entity]
[Automapping]
[Id('FID', TIdGenerator.Uuid38)]
TPerson = class
private
  FID: string;
  FName: string;
public
  property ID: string read FID write FID;
  property Name: string read FName write FName;
end;

And here is my code:

var Person := TPerson.Create;
Person.ID := person_from_web.id; // Set Primary Key
Person.Name := person_from_web.name;

ObjectManager.Save(Person); // raises EObjectNotPersistent
ObjectManager.SaveOrUpdate(Person); // sends an update command 

When I set the primary key before saving, the ObjectManager.Save method raises an EObjectNotPersistent exception, and the ObjectManager.SaveOrUpdate method sends an update command to the database instead of inserting a new record.

Has there been any progress in handling this scenario?

Thank you for help.

If you intend to always provide the id manually for every object insert, instead of letting Aurelius automatically generate the id for you, then I suggest you modify the identifier to none:

[Id('FID', TIdGenerator.None)]
TPerson = class

In this case you will be responsible for always providing the id of all objects. and SaveOrUpdate will not work because Aurelius cannot tell if it's an insert or update.

If you want to just sporadically set an id and do automatically insert or update in the database depending if the record already exists in the database, you can use replicate:

var Person := TPerson.Create;
Person.ID := person_from_web.id; // Set Primary Key
Person.Name := person_from_web.name;

ObjectManager.Replicate(Person); 

This will do all the work, but it's slower as it requires an additional SELECT to check if the record exists in the database.

I also suggest you always create a new topic here in Support Center, instead of replying to an old topic.