问题
FireDAC is returning a truncated message in TClientDataset.OnReconcileError
. The message is
[FireDAC][Phys][ODBC][Microsoft][SQL Server Native Client 11.0][SQL Server]The INSERT statement conflicted with the FOREIGN KEY constraint "FK_NAME". The conflict occurred in database "DATABASENAME", table "dbo.TABLENAME", column ''CD
The error comes from Datasnap.DBClient.TCustomClientDataSet.ReconcileCallback
, parameter pErrMessage. Call stack of the error:
ucdClientDataSet.TcdClientDataSet.cdInternalReconcile($11E17C90,$E8A8A70,ukInsert,raAbort)
Datasnap.DBClient.TCustomClientDataSet.ReconcileCallback(0,???,2,547,$11DE2990,$12038D90,$11F97F60,nil,$11F98B90,0,???)
MidasLib.DllGetDataSnapClassObject((202032032, 0, 0, (36, 191, 2, 18, 0, 134, 133, 14)),???,(no value))
:006f9a7b DllGetDataSnapClassObject + $1E32A
MidasLib.DllGetDataSnapClassObject((202032032, 0, 0, (36, 191, 2, 18, 0, 134, 133, 14)),???,(no value))
:006f9366 DllGetDataSnapClassObject + $1DC15
MidasLib.DllGetDataSnapClassObject((37671188, 1, 0, (0, 44, 2, 11, 25, 0, 0, 0)),(37671188, 1, 0, (192, 236, 145, 14, 100, 0, 0, 0)),(no value))
:006fa085 DllGetDataSnapClassObject + $1E934
Datasnap.DBClient.TCustomClientDataSet.Reconcile(???)
Datasnap.DBClient.TCustomClientDataSet.ApplyUpdates(0)
ucdClientDataSet.TcdClientDataSet.ApplyUpdates(0)
The message error code is 547. If I execute
select * from sysmessages where error = 547 and msglangid = 1033
in SQL Server, the message is:
The %ls statement conflicted with the %ls constraint "%.*ls". The conflict occurred in database "%.*ls", table "%.*ls"%ls%.*ls%ls.
Edit
To simulate the problem, here is it:
Create two tables in SQL Server (i'm using 12.0.2000.8), one referencing the other
CREATE TABLE TEST_TABLE_A(FIELD_PRIMARYKEY INT NOT NULL)
ALTER TABLE TEST_TABLE_A ADD CONSTRAINT PK_FIELD_PRIMARYKEY_1 PRIMARY KEY (FIELD_PRIMARYKEY)
CREATE TABLE TEST_TABLE_B(FIELD_FOREIGNKEY INT)
ALTER TABLE TEST_TABLE_B ADD CONSTRAINT FK_FIELD_FOREIGNKEY FOREIGN KEY (FIELD_FOREIGNKEY) REFERENCES TEST_TABLE_A(FIELD_PRIMARYKEY)
In Delphi (10.1 update 2), create a project and add:
TFDConnection: configure the connection in the database where the test table was created;
TFDQuery: connect with the FDConnection, and put the SQL text: "select * from TEST_TABLE_B";
TDatasetProvider: connect the Dataset property to the FDQuery;
TClientDataset: connect the ProviderName property to the Provider;
TButton: put the code:
ClientDataSet1.open;
ClientDataSet1.insert;
ClientDataSet1.fieldbyname('FIELD_FOREIGNKEY').asinteger := -1;
ClientDataSet1.post;
ClientDataSet1.ApplyUpdates(0);
In ClientDataset.OnReconcileError, add the code:
try
E.RaiseOuterException(Exception.Create('Error'+#13+#13+E.Message));
finally
Action := raAbort;
end;
Error message:
Error
[FireDAC][Phys][ODBC][Microsoft][SQL Server Native Client 11.0][SQL Server]The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FIELD_FOREIGNKEY". The conflict occurred in database "DBBALSAS_TESTE_PE", table "dbo.TEST_TABLE_A", column 'FIEL.
I did the same insert, but insert a FDQuery directly, and the error was complete:
FDQuery1.SQL.Text := 'INSERT INTO TEST_TABLE_B(FIELD_FOREIGNKEY) VALUES(-1)';
FDQuery1.ExecSQL;
[FireDAC][Phys][ODBC][Microsoft][SQL Server Native Client 11.0][SQL Server]The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FIELD_FOREIGNKEY". The conflict occurred in database "DBBALSAS_TESTE_PE", table "dbo.TEST_TABLE_A", column 'FIELD_PRIMARYKEY'.
来源:https://stackoverflow.com/questions/49681070/firedac-truncated-error-message-in-sql-server