Not getting the hang of this ManyValuedAssociation

I have my objects defined


   [Entity]
  [Automapping]
  [Sequence('SYS_GEN')]
  [Id('FId', TIdGenerator.IdentityOrSequence)] 
  TToken = class
  private
    FId: Integer;
    FTokenValue: string;
    FCreated: TDateTime
  public
   property Id: Integer read FId write FId;
   property TokenValue: String read FTokenValue write FTokenValue;
   property Created: TDateTime read FCreated write FCreated;
  end;

  [Entity]
  [Automapping]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TProfile = class
  private
   FId: Integer;  
   FName: String;

   [ManyValuedAssociation([TAssociationProp.Required], CascadeTypeAllRemoveOrphan)]
   [ForeignJoinColumn('PROFILE_ID', [TColumnProp.Required])]
   FTokens: TList<TToken>;
  public
   property Id: Integer read FId write FId;
   property Name: String read FName write FName;
   property Tokens: TList<TToken> read FTokens write FToken;
  end;

This seems to create the correct database structure with the filed 'PROFILE_ID' in the TOKEN table and a foreign key as expected.

CONSTRAINT FK_AUTH_TOKEN_PROFILE_PROFILE_ FOREIGN KEY (PROFILE_ID) REFERENCES PROFILE (ID)


In my application I have 2 Aureulius datasets dsProfile and dsToken. I have loaded the fields from a package built with the entities and I set the dsToken.DatasetField to the appropriate field from dsProfile.

However when I append a record to dsToken and post the changes I get 

exception class ESQLiteException with message 'NOT NULL constraint failed: TOKEN.PROFILE_ID'

From the documents it's not clear what else I need to do, unless I need to add an Association to TToken, but the documents seem to say you only need to do that if you want a two way relationship, which isn't necessary in this case.

Thanks

The ForeignJoinColumn creates a column in TToken (Profile_ID). Because you use TColumnProp.Required the column in TToken must not be nil. When dsToken appends the record does not really know how to fill the Profile_ID column.


I believe if you need to try the other options in ForeignJoinColumn

Also, try setting tbProfile.ParentManager to False. This  "clears" the manager associated with the detail table (which by default uses the same manager as the parent table). When there is a manager associated with the TAureliusDataset, it will perform immediate database operations upon table modifications. If you Post a record, it will call Manager.Save. 


As you don't have bidirectional association and Profile_ID is required, you can't directly save a TProfile entity. Without the manager, the dataset will only create the object and add it to the list, but not call Manager.Save. The profiles will then only be saved when the token is saved.