Error: column XX cannot be repeated

Hi,

Column ADDRESSTYPEID is repeated in the insert statement.

===================================================================
INSERT INTO AddressTypeDescriptions (
  ADDRESSTYPEDESCRIPTION, ROWVERSION, ADDRESSTYPEID, LANGUAGEID, ADDRESSTYPEID)
 VALUES (
  :A_ADDRESSTYPEDESCRIPTION, :A_ROWVERSION, :A_ADDRESSTYPEID, :A_LANGUAGEID, :A_ADDRESSTYPEID) RETURNING ID
A_ADDRESSTYPEDESCRIPTION = "TestAdresTypeNederlands" (ftString)
A_ROWVERSION = "1/01/1900" (ftDateTime)
A_ADDRESSTYPEID = NULL (ftInteger)
A_LANGUAGEID = "1" (ftInteger)
A_ADDRESSTYPEID = "14" (ftInteger)
===================================================================


Entities
---------

type

  TAddressTypeDescription= class;

  [Entity]
  [Table('AddressTypes')]
  TAddressType = class (TBaseWithID)
  private
    [Column('ROWVERSION', [])]
    [Description('')]
    FRowVersion: Nullable<TDateTime>;

    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
    [ForeignJoinColumn('ADDRESSTYPEID')]
    [ManyValuedAssociation([TAssociationProp.Lazy, TAssociationProp.Required], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FADDRESSTYPEID')]
    FAddressTypeDescriptions: Proxy<TList<TAddressTypeDescription>>;

    function GetAddressTypeDescriptions: TList<TAddressTypeDescription>;
  public
    constructor Create;
    destructor Destroy; override;

    property AddressTypeDescriptions: TList<TAddressTypeDescription> read GetAddressTypeDescriptions;
    property RowVersion: Nullable<TDateTime> read FRowversion write FRowversion;
  end;

  [Entity]
  [Table('AddressTypeDescriptions')]
  [UniqueKey('LANGUAGEID, ADDRESSTYPEID')]
  TAddressTypeDescription = class (TBaseWithID)
  private
    [Column('ADDRESSTYPEDESCRIPTION', [], 100)]
    FAddressTypeDescription: Nullable<string>;

    [Column('ROWVERSION', [])]
    FRowVersion: Nullable<TDateTime>;

    [Association([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('ADDRESSTYPEID', [TColumnProp.Required], 'ID')]
    FAddressType: Proxy<TAddressType>;

    [Association([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
    [JoinColumn('LANGUAGEID', [TColumnProp.Required], 'ID')]
    [Description('')]
    FLanguage: Proxy<TLanguage>;

    function GetLanguage: TLanguage;
    procedure SetLanguage(const Value: TLanguage);
    function GetAddressType: TAddressType;
    procedure SetAddressType(const Value: TAddressType);
  public
    constructor Create;
    property AddressTypeDescription: Nullable<string> read FAddresstypedescription write FAddresstypedescription;
    property RowVersion: Nullable<TDateTime> read FRowversion write FRowversion;
    property AddressType: TAddressType read GetAddressType write SetAddressType;
    property Language: TLanguage read GetLanguage write SetLanguage;
  end;


Code I use to get the error:

procedure TForm1.btnLanguageClick(Sender: TObject);
var
  oMng: TObjectManager;
  oAddressType: TAddressType;
  oDescr: TAddressTypeDescription;
begin
  oMng:= TObjectManager.Create(Connection);
  try
    oAddressType := TAddressType.Create;
    oDescr := TAddressTypeDescription.Create;

    oDescr.AddressTypeDescription.Value := 'TestAdresTypeNederlands';
    oDescr.Language := oMng.Find<TLanguage>(1);

    oAddressType.AddressTypeDescriptions.Add(oDescr);


    oMng.SaveOrUpdate(oAddressType);  //--> error  cannot be repeated in INSERT
    oMng.Flush;

 finally
    oMng.Free;
  end;
end;

What I am doing wrong?







Hi Christophe,


the problem is this mapping:



    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
    [ForeignJoinColumn('ADDRESSTYPEID')]
    [ManyValuedAssociation([TAssociationProp.Lazy, TAssociationProp.Required], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FADDRESSTYPEID')]
    FAddressTypeDescriptions: Proxy<TList<TAddressTypeDescription>>;


If you have a bidirectional association, the last parameter of ManyValuedAssociation attribute must be the field name of the other side of association (which must be FADDRESSTYPE, since you have no FADDRESSTYPEID field in class TAddressTypeDescription.
Once you do that, Aurelius will use the columns declared in that field, thus you don't need to redeclare it again using ForeignJoinColumn. This is the mapping that should work:



    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll)]
    [ManyValuedAssociation([TAssociationProp.Lazy, TAssociationProp.Required], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FADDRESSTYPE')]
    FAddressTypeDescriptions: Proxy<TList<TAddressTypeDescription>>;