Property not fetched

Hello!

I have a strange problem with subproperty fetching. I have those entites (simplified):slight_smile:

  [Entity]
  [Table('TipVozila')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  [EntityAuthorize]
  TTipVozila = class
  private
    [Column('Id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;

    [Column('Opis', [], 50)]
    FOpis: Nullable<string>;
  public
    property Id: Integer read FId write FId;
    property Opis: Nullable<string> read FOpis write FOpis;
  end;

  [Entity]
  [Table('Vozilo')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  [EntityAuthorize]
  TVozilo = class
  private
    [Column('Id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;

    [Association([TAssociationProp.Lazy], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('TipVozila', [], 'Id')]
    FTipVozila: Proxy<TTipVozila>;
  ...

If I start the application and execute this query

  crit := FManager.Find<TPot>;
  if Filter<>'' then begin
    crit.Where(
      Dic.Pot.Vozilo.RegistrskaSt.Like(L+Filter+'%')
      or Dic.Pot.Vozilo.Opombe.Like(L+Filter+'%')
      or Dic.Pot.Opis.Like(L+Filter+'%')
    );
  end;

  crit.OrderBy('Vozilo');
  crit.Refreshing; // Da ne dela samo s cache-om

  crit.FetchEager('Vozilo');
  crit.FetchEager('Vozilo.TipVozila');

  adsView.SetSourceCriteria(crit);
  adsView.Open;

Then I get the TVozilo entities that have TipVozila set as nil and if I try to edit one, I get access violation:

image

  if itm.Vozilo.TipVozila.Id = Ord(tvNorba) then begin  // <--  Access violation, TipVozila not assigned
    frmEditPotNorba := TfrmEditPotNorba.Create(self, itm); 
    frmEditPotNorba.ShowModal;

But if I open the application and first execute this:

  crit := FManager.Find<TVozilo>;
  crit.OrderBy('RegistrskaSt');
  crit.FetchEager('TipVozila');

Then the error does not appear on the process described above. I wonder what am I doing wrong.

Why are the TipVozila properties fetched in the first query and not in the second case?

Kind regards!

Are you sure all TipVozila are supposed to be retrieved as not nil? If there is indeed a record where they are set to nil, then you should check for nil regardless if it was retrieved eagerly or lazily.

You are getting an Access Violation when reading itm.Vozilo.TipVozila so you are reading the property directly and it means the property getter is being executed and even if it was not loaded eagerly it would be loaded lazily (I'm guessing, you didn't provide the declaration of the property).

You can confirm it by setting MaxEagerFetchDepth to a higher value, for example 5. If the error still happens, it likely means your TipVozila is supposed to be nil anyway.

Hello!

I tried to increase the MaxEagerFetchDepth, but no change.

The property should be filled and it's in the DB. And also if I execute the "other" query before the problematic one, the property is filled with correct data (for the same object).

I'm quite sure it's something that I did wrong, but just can't find what.

BTW: The entities are created using Data modeler, if this helps in any way.

Here is the inspected object form the other query

As I suspected it's not a problem with eager fetching, and changing MaxEagerFetchDepth didn't solve it, so it confirmed it.

I would ask for a minimal sample project reproducing the issue, so we can investigate at our side, I'm out of ideas right now.