Saving one-to-one property saves just the main class

Hello!

I have a problem when saving a class thah has one-to-one relation. It saves just the TStockItem class, but none of the classes that are in one-to-one fields of the main class. No errors during save.

I'm sure that it's me doing something wrong, but I just can't find what :frowning:

Please could you help?

Code (simplified) that produces the problem:

  StockItem.Prices.DateChanged := Now;
  try
    if StockItem.Id<1 then begin
      StockItem.EntryDate := now;
      FManager.Save(StockItem);
    end;
    FManager.Flush(StockItem);
  except
    on E: Exception do begin
      StockItem.Errors.Add('Interna napaka: '+E.Message);
    end;
  end;

Class definitions

  [Entity]
  [Table('product_prices')]
  [Id('FId', TIdGenerator.None)]
  TStockItemPrice = class(TBaseObject)
  private
    [Association([], CascadeTypeAllButRemove)]
    [JoinColumn('PROD_ID', [TColumnProp.Unique, TColumnProp.Required, TColumnProp.NoUpdate], 'ID')]
    FId: Proxy<TStockItem>;
    ...
    function GetId: TStockItem;
    procedure SetId(const Value: TStockItem);
  public
  	property Item: TStockItem read GetId write SetId;
  end;

function TStockItemPrice.GetId: TStockItem;
begin
  Result := FId.Value;
end;

procedure TStockItemPrice.SetId(const Value: TStockItem);
begin
  FId.Value := Value;
end;
  [Entity]
  [Table('products')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TStockItem = class(TBaseObject)
  private
    [Column('ID')]
    FId: integer;

    [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan, 'FId')]
    FPrices: Proxy<TList<TStockItemPrice>>;
    function GetPrices: TStockItemPrice;
    procedure SetPrices(const Value: TStockItemPrice);
  public
    constructor Create; reintroduce;
    destructor Destroy; override;

    property Id: integer read FId write FId;

    property Prices: TStockItemPrice read GetPrices write SetPrices;
  end;

function TStockItem.GetPrices: TStockItemPrice;
begin
  if FPrices.Value.Count > 0 then
    Result := FPrices.Value[0]
  else
    Result := nil;
end;

procedure TStockItem.SetPrices(const Value: TStockItemPrice);
begin
  if FPrices.Value.Count = 0 then
    FPrices.Value.Add(Value)
  else
    FPrices.Value[0] := Value;
end;

Database definition:

CREATE TABLE `products` (
	`ID` INT(10) UNSIGNED AUTO_INCREMENT NOT NULL,
	PRIMARY KEY (`ID`) USING BTREE
);

CREATE TABLE `product_prices` (
	`PROD_ID` INT(10) UNSIGNED NOT NULL,
	`PRICE_RETAIL` FLOAT(15,4) UNSIGNED NOT NULL DEFAULT '0.0000',
	`DATE_CHANGE` TIMESTAMP NOT NULL DEFAULT current_timestamp(),
	PRIMARY KEY (`PROD_ID`)
);

Update:

I attached a SQL monitoring. I see that Aurelius is trying to update product_prices instead of saving it. This makes me suspect that the problem lies in the definition of the class TStockItemPrice, but still no clue what am I doing wrong...

2023-06-09 10:31:46   Debug  Aurelius: INSERT INTO products (
  TYPE_ID, PARTNER_ID, MANUFACTURER_ID, DESCRIPTION, EAN, IS_ACTIVE, IS_SERVICE, LOCATION, NAME, PARTNER_PROD_ID, EM, DATE_ENTRY, SELL_FACTOR, EM2, BOM, LOT_NUMBER, PARAM1, PARAM2, PARAM3, PARAM4, PARAM5, PARAM6)
 VALUES (
  :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18, :p19, :p20, :p21, :p22)
2023-06-09 10:31:46   Debug  Aurelius: p1 = 1
2023-06-09 10:31:46   Debug  Aurelius: p2 = 2
2023-06-09 10:31:46   Debug  Aurelius: p3 = 2
2023-06-09 10:31:46   Debug  Aurelius: p4 = 
2023-06-09 10:31:46   Debug  Aurelius: p5 = 
2023-06-09 10:31:46   Debug  Aurelius: p6 = True
2023-06-09 10:31:46   Debug  Aurelius: p7 = True
2023-06-09 10:31:46   Debug  Aurelius: p8 = 
2023-06-09 10:31:46   Debug  Aurelius: p9 = TEST PRODUCT
2023-06-09 10:31:46   Debug  Aurelius: p10 = 
2023-06-09 10:31:46   Debug  Aurelius: p11 = kom
2023-06-09 10:31:46   Debug  Aurelius: p12 = 9. 06. 2023 10:31:46
2023-06-09 10:31:46   Debug  Aurelius: p13 = 1
2023-06-09 10:31:46   Debug  Aurelius: p14 = 
2023-06-09 10:31:46   Debug  Aurelius: p15 = 0
2023-06-09 10:31:46   Debug  Aurelius: p16 = 
2023-06-09 10:31:46   Debug  Aurelius: p17 = 
2023-06-09 10:31:46   Debug  Aurelius: p18 = 
2023-06-09 10:31:46   Debug  Aurelius: p19 = 
2023-06-09 10:31:46   Debug  Aurelius: p20 = 
2023-06-09 10:31:46   Debug  Aurelius: p21 = 
2023-06-09 10:31:46   Debug  Aurelius: p22 = 
2023-06-09 10:31:46   Debug  Aurelius: SELECT LAST_INSERT_ID()
2023-06-09 10:31:46   Debug  Aurelius: UPDATE product_prices SET 
  PROD_ID = :p1
WHERE PROD_ID = :p_1
2023-06-09 10:31:46   Debug  Aurelius: p1 = 8
2023-06-09 10:31:46   Debug  Aurelius: p_1 = 8

Please refer to:

1 Like

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