Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert records in master/detail relationship
    text
    copied!<p>I have two tables:</p> <p>OutputPackages (master)</p> <pre><code>|PackageID| </code></pre> <p>OutputItems (detail)</p> <pre><code>|ItemID|PackageID| </code></pre> <p>OutputItems has an index called 'idxPackage' set on the PackageID column. ItemID is set to auto increment.</p> <p>Here's the code I'm using to insert masters/details into these tables:</p> <pre><code>//fill packages table for i := 1 to 10 do begin Package := TfPackage(dlgSummary.fcPackageForms.Forms[i]); if Package.PackageLoaded then begin with tblOutputPackages do begin Insert; FieldByName('PackageID').AsInteger := Package.ourNum; FieldByName('Description').AsString := Package.Title; FieldByName('Total').AsCurrency := Package.Total; Post; end; //fill items table for ii := 1 to 10 do begin Item := TfPackagedItemEdit(Package.fc.Forms[ii]); if Item.Activated then begin with tblOutputItems do begin Append; FieldByName('PackageID').AsInteger := Package.ourNum; FieldByName('Description').AsString := Item.Description; FieldByName('Comment').AsString := Item.Comment; FieldByName('Price').AsCurrency := Item.Price; Post; //this causes the primary key exception end; end; end; end; </code></pre> <p>This works fine as long as I don't mess with the MasterSource/MasterFields properties in the IDE. But once I set it, and run this code I get an error that says I've got a duplicate primary key 'ItemID'. </p> <p>I'm not sure what's going on - this is my first foray into master/detail, so something may be setup wrong. I'm using ComponentAce's Absolute Database for this project.</p> <p>How can I get this to insert properly?</p> <p><strong>Update</strong></p> <p>Ok, I removed the primary key restraint in my db, and I see that for some reason, the autoincrement feature of the OutputItems table isn't working like I expected. Here's how the OutputItems table looks after running the above code:</p> <pre><code>ItemID|PackageID| 1 |1 | 1 |1 | 2 |2 | 2 |2 | </code></pre> <p>I still don't see why all the ItemID values aren't unique.... Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload