[yunqa.de] Re: How to prevent "Key Violation" errors with ClientDataSet

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Sat, 20 Feb 2010 11:23:34 +0100

At 23:30 19.02.2010, Todd Cary wrote:

>I am using the DISQLite3 Personal package and all is working fine, however I 
>added
>
>procedure TFormMain.ClientDataSetNewRecord(DataSet: TDataSet);
>begin
>  DataSet.Fields[0].ReadOnly := False;
>  DataSet.Fields[0].AsInteger := FID;  //FID is negative
>  Dec(FID);
>end;
>
>so that I will not get the Key Violation errors from my key field, ID.  
>In looking at the Demo code for the World ClientDataSet application, I cannot 
>find how it avoids the Key Violation since it does not have the OnNewRecord.

There are no key violations because TDISQLite3UniDirQuery exludes pfInUpdate 
from ftAutoInc fields. The code is in TDISQLite3UniDirQuery.InternalOpen.

The Countries.ID field in World.db3 is carefully declared as INTEGER PRIMARY 
KEY. TDISQLite3UniDirQuery resolves this to a ftAutoInc field and then removes 
pfIinUpdate from it. As a result, TClientDataSet does not generate SQL code for 
this field when writing it to the database. Hence, DISQLite3 fills in the 
appropriate values and there are no key violations.

Your suggestion is another solution to the same TClientDataSet problem. 
However, it should not be needed for DISQLite3 if primary keys are properly 
declared as INTEGER PRIMARY KEY.

Ralf  

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: