ID not set on entity class and sequencer definitions

When i try to execute this:

   MyCriteria := aObjectManager.Find<TEmployee>;
   aList := MyCriteria.List;

I get this error "ID not set on entity of class TGrade"

This is basic class definition

[Entity]
  [Table('employee')]
  [Description('')]
  [Sequence('employee_id_seq')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TEmployee = class(TMoonshineObject)
  private
    [Column('id', [TColumnProp.Required])]
    [Description('')]
    FId: Integer;
...
end;

I found that problem is in an record which ID = 0.
Why we can't have records with ID = 0 ?
In our case it is by design this table sequencer sequencer is defined to start from 0.

CREATE SEQUENCE public.employee_id_seq
	INCREMENT BY 1
	MINVALUE 0
	MAXVALUE 2147483647
	START 0
	CACHE 1
	NO CYCLE;

It is an old application which we rewrite now using Aurelius.
Isn't more correct to check how sequencer for table is defined and to give errors only if ID doesn't match sequencer declaration ?

You can. Use the IdUnsavedValue attribute to set a default 'unsaved id' different from zero.

  [Entity]
  [IdUnsavedValue(-1)]  // <-- insert this line. 
  [Table('employee')]
  [Description('')]
  [Sequence('employee_id_seq')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TEmployee = class(TMoonshineObject)
  (***)

HTH,

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.