Using virtual foreign keys
From DreamFactory
Contents
- 1 Tutorial
- 2 API Endpoint
- 3 API Docs Screenshot
- 4 Example - Fetch contact and contact_info_by_contact_id records in one API call.
- 5 Example - Fetch contact, contact_info_by_contact_id, and contact_group_relationship_by_contact_id records in one API call.
- 6 Example - Fetch contact and all related records by foreign key in one API call.
Tutorial
"Virtual foreign keys" are a powerful feature that allow you to impose virtual foreign key relationships on data in unrelated tables in the same database or unrelated tables in totally different databases, without touching the database schema at all. This is a super convenient way to orchestrate complex queries on disparate data without having to hand-code server-side logic.
Once you've set up a virtual foreign key relationship, it's easy to perform CRUD operations that read, write, update, and delete records on the "virtually" related data, all with a single API call that passes the virtual foreign key as an API parameter. Try these examples in the 'API Docs' tab of the DreamFactory Admin Console or from the command line with cURL.
API Endpoint
GET https://{url}/api/v2/{api_name}/_table/{table_name}?related={foreign_key_field}
API Docs Screenshot
Example - Fetch contact and contact_info_by_contact_id records in one API call.
- Table name: contact
- Related parameter in API call:
contact_info_by_contact_id
- Request URL:
https://foo.com/api/v2/db/_table/contact?related=contact_info_by_contact_id
Example - Fetch contact, contact_info_by_contact_id, and contact_group_relationship_by_contact_id records in one API call.
- Table name: contact
- Related parameter in API call:
contact_info_by_contact_id, contact_group_relationship_by_contact_id
- Request URL:
https://foo.com/api/v2/db/_table/contact?related=contact_info_by_contact_id%2C%20contact_group_relationship_by_contact_id
- Table name: contact
- Related parameter in API call:
*
- Request URL:
https://foo.com/api/v2/db/_table/contact?related=*