#1 2025-09-10 08:03:45

testgary
Member
Registered: 2025-02-06
Posts: 29

stored AS_UNIQUE

mongodb+windows+lazarus

  TORMTest= class(TORM)
  private
    FIdempotency_Key: RawUtf8;
  public
    class procedure InitializeTable(const Server: IRestOrmServer; const FieldName: RawUtf8; Options: TOrmInitializeTableOptions); override;
  published
    property Idempotency_Key: RawUtf8 read FIdempotency_Key write FIdempotency_Key stored AS_UNIQUE; 
  end;  

class procedure TORMTest.InitializeTable(const Server: IRestOrmServer; const FieldName: RawUtf8; Options: TOrmInitializeTableOptions);
var
  B: boolean;
begin
  inherited InitializeTable(Server, FieldName, Options);
  // B := Server.CreateSqlIndex(TORMTest, ['User_ID', 'create_time', 'update_time', 'subject'], False);
  // B := Server.CreateSqlIndex(TORMTest, 'Idempotency_Key', True);
  B := Server.CreateSqlMultiIndex(TORMTest, ['Idempotency_Key'], True);
end; 

Why isn't Idempotency_Key a unique index? Did I make a mistake somewhere?

Last edited by testgary (2025-09-10 15:16:06)

Offline

#2 2025-09-10 18:07:08

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

Re: stored AS_UNIQUE

Why do you say it is not unique?

It should go into TMongoCollection.EnsureIndex() then create the index if needed.
What does this method returns?
What are the logs? (always ensure you enabled the logs and provide them as a link here to investigate)

Offline

#3 2025-09-11 07:10:09

testgary
Member
Registered: 2025-02-06
Posts: 29

Re: stored AS_UNIQUE

class procedure TORMTest.InitializeTable(const Server: IRestOrmServer; const FieldName: RawUtf8; Options: TOrmInitializeTableOptions);
begin
  // inherited InitializeTable(Server, FieldName, Options);
  Server.CreateSqlIndex(TORMTest, ['User_ID', 'create_time', 'update_time', 'subject'], False);
  Server.CreateSqlIndex(TORMTest, 'Idempotency_Key', True);
end;

I just checked and found that commenting out "inherited" would work. As for whether to include "stored AS_UNIQUE", it seems to make no difference. I haven't written the log yet.

Offline

Board footer

Powered by FluxBB