#1 2026-08-19 09:47:05

augusto
Member
Registered: 2017-01-16
Posts: 6

MSSQL table with an IDENTITY field issue

Hi All,

Please help.

I'm having trouble to get mORmot(2.4) ORM insert working with an existing MSSQL table with an IDENTITY field.

when I run

NewID := Server.Add(Rec, true);

It runs

"select max(ID) from dbo.RemoteProcessTest"

...and insists on sending the ID column in the insert statement - "insert into dbo.RemoteProcessTest (ID,ProcessId,Title) values (?,?,?))"
- then get an error :

Cannot insert explicit value for identity column in table 'RemoteProcessTest' when IDENTITY_INSERT is set to OFF

I'm not sure if this is a bug or if I'm missing something in my configuration.


Table:

CREATE TABLE dbo.RemoteProcessTest
(
    ID int IDENTITY(10000,1) NOT NULL,
    ProcessId varchar(40) NOT NULL,
    Title varchar(40) NOT NULL,

    CONSTRAINT PK_RemoteProcessTest
    PRIMARY KEY(ID)
); 

Offline

#2 2026-08-19 14:43:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,595
Website

Re: MSSQL table with an IDENTITY field issue

Please follow the forum rules and put your code in external links like gist.
Especially when the code is not self-containing (no table init).

In a quick look, I would say that MSSQL table with an IDENTITY field is not supported by the ORM.

Offline

#3 2026-08-19 15:25:48

augusto
Member
Registered: 2017-01-16
Posts: 6

Re: MSSQL table with an IDENTITY field issue

Thanks ab
Sorry for this, here is the link:
https://gist.github.com/MRIAG2026/dd69f … e0b2165465

in this case, I guess that if the MSSQL table with IDENTITY field is not supported by the ORM,  I will always have to call Execute/ExecuteInlined directly from TSQLDBConnectionProperties for inserting records.
Is this feature planned to be added in the future?

Offline

#4 2026-08-19 15:48:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,595
Website

Re: MSSQL table with an IDENTITY field issue

Yes, you are right: use direct SQL for such tables.

This feature is clearly not planned at all from our side.
But you are free to contribute and send some Pull Request.

Offline

#5 2026-08-20 06:19:21

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 96

Re: MSSQL table with an IDENTITY field issue

Hi, in my humble experience, for old tables i had to add another field as OrmId and set mormot's ID fieldname to this. So, sql and other softwares can keep running as they were and mormot can do it's job as intented.

Offline

#6 2026-08-20 06:21:05

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,595
Website

Re: MSSQL table with an IDENTITY field issue

Good workaround.

Offline

#7 2026-08-20 14:54:00

augusto
Member
Registered: 2017-01-16
Posts: 6

Re: MSSQL table with an IDENTITY field issue

Thank both for your help.

Yes, that's a good workaround if you are able to change the table structure.

Also found that setting the EngineAddForcedID property on the TRestStorageExternal instance, does the trick. That way, resulting insert statement doesn't include the ID field.
see example : https://gist.github.com/MRIAG2026/fe2aa … 14fa0dfa68

Offline

Board footer

Powered by FluxBB