#1 Re: mORMot 1 » How to create new entity with linked entity? » 2015-03-30 11:23:25

lele9,

lele9 wrote:

vSession.SystemUser := vUser.ID;

I've publish property of a type TSystemUser, not TId, so I need to assign instance of TSystemUser to it.

ab,

ab wrote:
vSession.SystemUser := vUser.AsTSQLRecord;

See http://synopse.info/files/html/Synopse% … ml#TITL_70

So you should store the ID in the published property, not an instance:

vSession.SystemUser := vUser.AsTSQLRecord;

Thank you very much! Works like a charm!

Alexander.

#2 mORMot 1 » How to create new entity with linked entity? » 2015-03-30 09:47:19

volax
Replies: 3

Hi!

I'm stuck in creating new entity with link to already existing entity of other type.

My simplified model:

  TSystemUser = class(TSQLRecord)
  private
    fUserName: string;
  published
    property UserName: string read fUserName write fUserName;
  end;

  TSystemSession = class(TSQLRecord)
  private
    fSystemUser: TSystemUser;
    fStartTime: TDateTime;
  published
    property SystemUser: TSystemUser read fSystemUser write fSystemUser;
    property StartTime: TDateTime read fStartTime write fStartTime;
  end;

I'm loading user from DB (PostgreSQL with ZEOS) and creating user session like this:

var
  vUser: TSystemUser;
  vSession: TSystemSession;
begin
  vUser := TSystemUser.CreateAndFillPrepare(fRestServer, 'username=?', ['volax']);
  try
    if vUser.FillOne then
    begin
      vSession := TSystemSession.Create;
      try
        vSession.StartTime := Now;
        vSession.SystemUser := vUser;
        // Adding user's session:
        fRestServer.Add(vSession, True);
      finally
        vSession.Free;
      end;
    end;
  finally
    vUser.Free;
  end;

After call to fRestServer.Add(vSession, True); the session created, but value of SystemUser field in SystemSession table looks to be pseudo-random value, StartTime seems to be correct.

Can anyone explain, what's wrong with my code?

Thank you!

#3 Re: mORMot 1 » ZEOS+Postgresql CreateMultiIndex » 2015-03-16 11:45:36

Hi, all,

chapa wrote:

Next problem is that CreateSQLMultiIndex throws an exception on second program run.
As far as I know, there is no statement IF NOT EXISTS for index creation in Postgresql, hope I am wrong.

The problem with CreateSQLMultiIndex is in call to TSQLDBConnectionProperties.SQLAddIndex function, which generates index name.
TSQLDBConnectionProperties.SQLAddIndex contains a bug:

    if length(FieldsCSV)+length(Table)>27 then
      // sounds like if some DB limit the identifier length to 32 chars
      IndexName := IndexName+'INDEX'+crc32cUTF8ToHex(Table)+
        crc32cUTF8ToHex(FieldsCSV)+CardinalToHex(GetTickCount64) // BUG! GetTickCount64 randomizes index name!
    else
      IndexName := IndexName+'NDX'+Table+FieldsCSV;

Call to GetTickCount64 "randomizes" index name, so new indexes created again and again at every server restart, caused by failed index existence checks.
Removing "+CardinalToHex(GetTickCount64)" part of IndexName will do the trick:

    if length(FieldsCSV)+length(Table)>27 then
      // sounds like if some DB limit the identifier length to 32 chars
      IndexName := IndexName+'INDEX'+crc32cUTF8ToHex(Table)+
        crc32cUTF8ToHex(FieldsCSV)
    else
      IndexName := IndexName+'NDX'+Table+FieldsCSV;

Arnaud, is it possible to fix?

Thank you!
Best regards, Alexander.

Board footer

Powered by FluxBB