[Lazarus] Master/detail relationships: what am I missing?

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Mon Dec 17 11:07:38 CET 2012



On Mon, 17 Dec 2012, Reinier Olislagers wrote:

> Hi all,
>
> I've been looking into how to use master/detail setups in Lazarus (aka
> tables with foreign keys).
>
> Based on Delphi docs I started writing up & testing this:
>> See also
>> http://docwiki.embarcadero.com/RADStudio/XE3/en/Establishing_Master-detail_Relationships_Using_Parameters
>> Use one connection, 1 transaction, but 2 queries, 2 data sources
>> Example from the Firebird EMPLOYEE database (as used in the SQLDB Tutorials):
>> - a CUSTOMER table with an integer primary key CUST_NO and other fields
>> - a SALES table with a CUST_NO integer field that is a foreign key linking to the CUST_NO field in the CUSTOMER table
>> - a master query selects from the CUSTOMER table
>> - a detail query that selects from the SALES table
>>
>> In the detail query:
>> - set the database property as usual
>> - set the datasource property to point to the master datasource
>> - in the query SQL, use a WHERE query to limit the select; use a parameter with the name of the field in the master table
>> SELECT * from SALES WHERE SALES.CUST_NO=:CUST_NO
>> FPC will now see this as a reference to the current value of CUST_NO in the CUSTOMER (master) query. This is made possible by the fact that the master datasource keeps track of the current record for the master query
>>
>> Make sure that the master query is open before the detail query so it can look up fields.
>>

>> No need for further code, e.g. to specify default values for the SALES.CUST_NO field when adding records.

This is wrong. There is a need. This should still be specified.

>
> I get the form with a combobox for the master field and details grid to
> run without issue and can insert items in the detail grid but the
> foreign key to the master table is null for that record.

Correct. You must enter these values yourself in the AfterInsert of the
detail dataset.

Michael.




More information about the Lazarus mailing list