Jump to content

Master-Detail Example

Recommended Posts

Does anybody know where I can find a (simple) example of setting up a Master-Detail on two tables (MSSQL in my case)? 


All I can find via the internet are either overly complicated examples or in non-English languages. The Help in Delphi/Firedac is the former and very difficult to follow.

Share this post

Link to post
Posted (edited)

You need a datasource that points to the master dataset.


The Detail dataset needs to have its Mastersource property pointing to the dataSource (this step needs to be followed for both of the options specified below).


You can then either:


  • use the object inspector for the Detail dataset to define the relationship with the Master table (you need to set IndexFieldNames, MasterFields and as already mentioned MasterSource)
  • or if the Detail dataset is an FDQuery you can define the relationship via a parameterized query entered into the SQL.Text property eg "SELECT [fields] FROM [mytable] WHERE [detail_table_foreign_key] = :[master_table_primary_key

So in the latter case, if you had a master table called "employers" with aprimary key called "e_id" and a detail table called "employees" with a foreign key called "employer_id" your Detail query would need to be as follows: 

SELECT * FROM employees WHERE employer_id = :e_id

The parameter is case sensitive. It must match precisely the name of the primary key field in the Master table.


Edited by Mark Williams

Share this post

Link to post

BTW I meant to mention that you would probably have got a faster reply to this question if you had posted it in the FireDac sub-forum in Databases.

Share this post

Link to post

Many thanks, thought this was just a data question rather than specific to FireDac, but noted for next time ...

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now