I have a foreign key problem that I can't understand.
I can work around this bug, but the price is high. Either we have to change every update that check data from other data,
we drop all foreign keys,
or set them to update cascade.
Create column table Header (idheader int primary key, SumQuantity decimal(18,6)); create column table Lines (idheader int, idline int, quantity decimal(18,6), primary key(idheader, idline)); alter table Lines add constraint FK_Lines_IDHeader FOREIGN KEY (IDHeader) REFERENCES Header(IDHeader); Insert into Header values (1, 0); insert into Lines values (1,0,2); insert into Lines values (1,1,5); --Does not work, return SAP DBTech JDBC: [2048]: column store error: [1536] On Update/Delete action failed on referencing Table;from table NWARETEST:HEADERen to table NWARETEST:LINESen, action=deindex, ref-action=restrict hit 2 rows in table NWARETEST:LINESen UPDATE Header SET SumQuantity = IFNULL((SELECT SUM(Quantity) FROM Lines WHERE Lines.IDHeader = Header.IDHeader), 0) WHERE Header.IDHeader = 1; --work Update Header SET SumQuantity = 7; --Drop the constraint alter table Lines drop constraint FK_Lines_IDHeader; --Work UPDATE Header SET SumQuantity = IFNULL((SELECT SUM(Quantity) FROM Lines WHERE Lines.IDHeader = Header.IDHeader), 0) WHERE Header.IDHeader = 1; --Recreate the constraint with the IDHeader primary key ON UPDATE CASCADE alter table Lines add constraint FK_Lines_IDHeader FOREIGN KEY (IDHeader) REFERENCES Header(IDHeader) on update cascade; --Work UPDATE Header SET SumQuantity = IFNULL((SELECT SUM(Quantity) FROM Lines WHERE Lines.IDHeader = Header.IDHeader), 0) WHERE Header.IDHeader = 1;
The query use the Lines table to calculate the total quantity and put that quantity in the header table.
The problem is, the system block the update. It is the same kind of error that if I did change the IDHeader on the header table.
the update does not change the header, just the SumQty, but the system block this. This is an error. It should not block this.
The workaround is to either drop the constraint, or set it to update cascase, This is not really pretty, we don't update the primary keys, but if we do it, we would like to have a choice to do it manually no update casdate.
If we don't set to update cascade, then we completly loose the foreign key functionnality.
Another example :
I can't find a way to reproduce it with an example, but
Table A, table B does not have a foreign key, table c has a foreign key to primary key table a. This is the same example of above except the new table c.
Same query as above, update table A set 1 field = sum of table b. The foreing of of table c block the update ?? This was my initial problem.
Can someone check if this still does not work on the lastest patch or if they have a better workaround ?