Questions on Fields

Hi Wagner,


I am in trouble with this code, I am posting here what I have for one specific table:



type
  TEntityDominio = class;


  TNaharEntity = class
  private
    FDominio: TEntityDominio;


  protected
    procedure SetDominio(Value: TEntityDominio);
    function  GetDominio: TEntityDominio;


  public
    constructor Create; virtual;
  end;


  [Entity]
  [Table('DOMINIO')]
  [Id('ID', TIdGenerator.None)]
  TEntityDominio = class(TNaharEntity)
  private
    FId: TGuid;
    FNome: string;


  private
    procedure SetNome(const Value: string);


  public
    constructor Create; override;


    [Column('ID', [TColumnProp.Unique, TColumnProp.NoUpdate])]
    property Id: TGuid read FId write FId;


    [Column('NOME', [TColumnProp.Required, TColumnProp.Unique], 20)]
    property Nome: string read FNome write SetNome;
  end;


implementation


{ TNaharEntity }


constructor TNaharEntity.Create;
begin
end;


function TNaharEntity.GetDominio: TEntityDominio;
begin
  result := FDominio;
end;


procedure TNaharEntity.SetDominio(Value: TEntityDominio);
begin
  FDominio := Value;
end;


{ TEntityDominio }


constructor TEntityDominio.Create;
begin
  inherited;


  FId := GUID_NAHAR_DOMAIN_DEFAULT;
end;


procedure TEntityDominio.SetNome(const Value: string);
begin
  FNome := Value;
end;


Problems that I have with this code:

1) It is raising a EUnexpectedFieldType: Nahar.Entity.TEntityDominio when creating the database

2) I have added the base class (that is not a table) TNaharEntity because I want to declare on the Constructor (that is empty right now) the DEFAUL TNaharDominio. 

I have explained this in a previous post, however for the sake of clarity: Dominio is a overall filter for almost all the tables. If I am working with the company 1 for example, everything needs to be selected based on the DOMINIO that was defined for company 1.

As you can see is a simple table. And you can see that the FDominio is declared on the base class. So all the other tablets that are decendant of this class will only define the property with the tag Column Dominio. The set and get are already defined and the default value would be pre defines already.

The fact is that Aurelius in some situations create the Class on its own, then I need to be sure that Dominio is always correct (I have a singleton that keeps it for me). The record can be written wrong in many ways, but never with the wrong dominio.

What should I do? Load a TEntityDominio and keep it in memory to make the attribution 
FDominio := FLoadedEntityDominio every time the base class is created?




  1. Is that the exact source code? Why would TEntityDominio be mapped to database if it doesn't have [Entity] attribute? Aurelius would ignore it

    2) I honestly don't know what would be the best approach. If you are linking to an object through association (dominio) then you must have the dominio object loaded in the same manager as you are using to deal with the entities. Or maybe your should just make those integer types (instead of object types) and save the raw value directly (do you need TDominio to be a class?). 
    Are you going to mix objects with different domains in the same manager?
  1. that is the exact code, i believe you mention the base class TNaharEntity. That class is not a table, it is a "helper" class that all entities descend from. Then I can initialize on the create (but I go stuck on that) 

    I have removed the constructor for a test and I remain getting the exception Unexpected Field Type: Nahar.Entity.TEntityDominio   that is the class/table I mentioned.

    How can I track where is the problem? What Aurelius is not liking? I am deleting the database and always making my code force a new database create.

    2) well, right now the manager is per model, and in my MVP it is mostly 1 view = 1 presenter = 1 model, so when the user closes the form the manager is gone. But the global context remains. I could change, as you mentioned to a interger for example, to make it simple, since this is a real special case

could it be because that first declaration of the class? 


TEntityDominio = class;

there is a circular reference on this classes, could it be aurelius is getting lost on that?

I am removing the circular reference, and changing the dominio to a integer.


I will be loosing the automatic joining, but that is not a big deal for the dominio.

Let´s see what will happen...


Well, changing the code to dominio = integer just works... what is pretty basic.


However I see that there is a problem with that circular reference solution (declaring the template class first) I could not figure out way. Since I have removed the dependency in the domino entity I have removed that class template and now aurelius understand and created this table.

THis is not critical, I will keep this way.

Hi Eduardo, I'm glad you solved the problem.

However, if you could send me a small project that reproduces that circular problem, I would appreciate it, so I can fix the problem. If you can, please use SQLite so I can easily compile and run it here.