Relations join together two datasources, for example a CUSTOMERS and a ORDERS table. Through RelationItems the nature of the link between the datasources is defined, for example to link the CUSTOMERS and ORDERS table based on the customer_id column.
Relations return a JSFoundSet object.
Relations are used in Solutions to display related data, for example using a [Field], a TabPanel or a Portal or to work with related data in business logic.
There are several possible Relation configuration that make a Relation special
Relations that contain only RelationItems based on global variables on the source side, thus no dependency on the source table are considered global relations.
Global relations can be used to get a limited FoundSet, for example all active customers, by adding a relation item that specifies that the "active" flag in the CUSTOMERS table has to be equal to a certain global variable. For example, if the CUSTOMERS table contains a column called "is_active" of type INTEGER and a default value of 1 indicating it is an active record, the following would create a global relation that returns a FoundSet with all active customers:
var ACTIVE = 1;
If the primary and foreign table are equal and the relation does not contain any RelationItems, it becomes a Container relation. A Container relation returns it originating FoundSet. A Container relation can be used to set the FoundSet of the parent form into the Form displayed in a TabPanel.
Relations can use the same table as primary and foreign table. Depending on the setup of the the RelationItems the relation can point back to the originating record or one or more different records.
Relations have several settings that influence the behavior of the Relation under different circumstances.
Allow creation of related records: This property indicates if the relation can be used to create new related records
true: the execution of "customers_to_orders.newRecord()" will create a new record.
false: trying to create a new record over the relation will throw a ServoyException.NO_RELATED_CREATE_ACCESS exception.
Allow parent delete when having related records: This property indicated if a record can be deleted if it has child records
true: when there are related order records, Servoy will block the delete of the customer record and raise a ServoyException.NO_PARENT_DELETE_WITH_RELATED_RECORDS exception.
false: related records will not block the delete of the parent record
Delete related records: This property indicates if child records should be automatically deleted when the parent record is deleted
true: when the customer record is deleted and there are related records in the orders table, those order records will also get deleted.
false: related records will remain untouched.
Note 1: The option Allow parent delete when having related records overrules the option Delete related records. This means that if the first option is set to false, the latter option is ignored. Setting the first option to false and the latter option to true will still block the delete.
Note 2: When an attempt is made to delete a record, each relation from the datasource (table) to which the record belongs will be checked to see if the delete is allowed. If one relation is found that blocks the delete, the entire delete will fail.
Note 3: When a relation has the Delete related records property set to true, an attempt is made to also delete all individual related records. On each individual related record the checks mentioned under Note 2 will be performed. If any of the related records blocks the delete, the entire delete is blocked.
Note 4: Deletes will continue to cascade if there are relations found on the record to be deleted that have the Delete related records property set to true.
Example 1: A simple scenario
This example defines three relations in the typical customer > orders > orderitems setup:
Relation 1: customer_to_orders, deleteRelatedRecords=true, allowParentDeleteWhenHavingRelatedRecords=false
Relation 2: order_to_orderitems, deleteRelatedRecords=true, allowParentDeleteWhenHavingRelatedRecords=false
Relation 3: orderitem_to_product, deleteRelatedRecords=false, allowParentDeleteWhenHavingRelatedRecords=false
If an attempt is made to delete a customer record which has order and the orders have orderitems, when the relations are setup as described above the customer record will be deleted, all the orders related to the customer are deleted and all orderitems related to the orders of the customer will be deleted.
As Relation 3 has the deleteRelatedRecords property set to false, no attempt will be made to delete the product record related to the orderitems that are to be deleted.
Example 2: Implementing business logic enforcement in the data layer
This example defines four relations starting with the in the typical customer > orders > orderitems setup, followed by the following business logic enforced by the datamodel: If an order resulted in an Invoice, the id of the invoice is stored on the order record in the invoice_id column. If an order has a related invoice then it cannot be deleted
Relation 1: customer_to_orders, allowParentDeleteWhenHavingRelatedRecords=false, deleteRelatedRecords=false
Relation 2: order_to_orderitems, allowParentDeleteWhenHavingRelatedRecords=false, deleteRelatedRecords=false
Relation 3: orderitem_to_product, allowParentDeleteWhenHavingRelatedRecords=false, deleteRelatedRecords=false
Relation 4: order_to_invoice, allowParentDeleteWhenHavingRelatedRecords=false
This setup is mostly the same as example 1, except for the additional fourth relation. This relation from the orders table to the invoices defines that the order record cannot be deleted if it has a related invoice record.
In this setup, when an attempt is made to delete a customer record the delete is blocked if the customer has a related order that has a related invoice record.
{column:width=80px}{column}{column}{column}
Property Summary
allowCreationRelatedRecords
Flag that tells if related records can be created through this relation.
allowParentDeleteWhenHavingRelatedRecords
Flag that tells if the parent record can be deleted while it has related records.
deleteRelatedRecords
Flag that tells if related records should be deleted or not when a parent record is deleted.
foreignTable
Qualified name of the foreign data source.
initialSort
A String which specified a set of sort options for the initial sorting of data
retrieved through this relation.
primaryTable
Qualified name of the primary data source.
{column:width=100%}{column}
Property Details
Flag that tells if related records can be created through this relation.
The default value of this flag is "false".
Parameters
Also see
External links
Sample
Flag that tells if the parent record can be deleted while it has related records.
The default value of this flag is "true".
Parameters
Also see
External links
Sample
Flag that tells if related records should be deleted or not when a parent record is deleted.
The default value of this flag is "false".
Parameters
Also see
External links
Sample
Qualified name of the foreign data source. Contains both the name of the foreign
server and the name of the foreign table.
Parameters
Also see
External links
Sample
A String which specified a set of sort options for the initial sorting of data
retrieved through this relation.
Has the form "column_name asc, another_column_name desc, ...".
Parameters
Also see
External links
Sample
The join type that is performed between the primary table and the foreign table.
Can be "inner join" or "left outer join".
Parameters
Also see
External links
Sample
The name of the relation.
Parameters
Also see
External links
Sample
Qualified name of the primary data source. Contains both the name of the primary server
and the name of the primary table.
Parameters
Also see
External links
Sample