#1 2022-03-02 18:16:40

Stemon63
Member
Registered: 2016-10-24
Posts: 50

Mormot2 - ID from Postgres Autoinc

Good evening to all,
I need to  assign the ID value directly in DB from an autoincremental value (ProstgreSQL). Database is shared with other applications.
There is a way for manage correctly the ORM Id so it can be assigned into DB and not as ( Select max id...) into TOrm class?

Thanks in advance!

Offline

#2 2022-03-03 07:58:50

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Mormot2 - ID from Postgres Autoinc

There are several ways to do it in TRestStorageExternal.

The TRestStorageExternal.OnEngineAddComputeID method is defined for computing an ID before the INSERT.
You can put here some low-level SQL statement to get an ID, e.g. from a DB sequence.

If you want to let the DB create the ID, you may just set TRestStorageExternal.EngineAddForcedID to a non 0 value.
But note that this "fake" value will be returned by the ORM, not the actual ID value as inserted by the DB.

Online

#3 2022-03-03 16:52:54

Stemon63
Member
Registered: 2016-10-24
Posts: 50

Re: Mormot2 - ID from Postgres Autoinc

Hi Thanks,
I need this:
ID should not be passed into the sql of the db when making an insert, letting the db assign it autonomously in the rows. Then I get it when I run a query (retrieve, etc) for updates. In practice I ask if it is possible an option not to assign the ID in the add, without querying the db to retrieve the last ID before making insertion (for avoiding conflict on multiapp inserts). Pratically  I'm looking for a way for don't assign ID in SQL insert.

Any help is welcome!
Thanks in advance

Offline

#4 2022-03-03 16:54:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Mormot2 - ID from Postgres Autoinc

This is what TRestStorageExternal.EngineAddForcedID is for.
Please read my text above, and the documentation.

Online

#5 2022-03-03 18:00:14

Stemon63
Member
Registered: 2016-10-24
Posts: 50

Re: Mormot2 - ID from Postgres Autoinc

Hi Ab,
I have read your text, but I don't find EngineAddForcedID in documentation.

I will try to find some information in code.

Thanks a lot

Offline

#6 2022-03-03 18:17:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Mormot2 - ID from Postgres Autoinc

The low-level documentation is written as part of the code, as comments.

Then they are extracted as
https://synopse.info/files/html/api-1.1 … C_45B30F67

Online

#7 2022-03-03 18:19:00

Stemon63
Member
Registered: 2016-10-24
Posts: 50

Re: Mormot2 - ID from Postgres Autoinc

Ah, Ok.
Thanks a lot!
Good job!

Offline

Board footer

Powered by FluxBB