Relations between SQL tables
Foreign key relations need to be defined in the database schema.
When an SQL table references another SQL table, you need to define a foreign constraint for the
referencing table. This information is also required in the DB Sync database schema, and must be
provided in
"references" property of table definitions: {
"ordinal": 9,
"name": "employee",
...
"references": [ ... ],
...
}The "references" record must contain the following properties:
name: The name of the foreign key constraint.cols: The list of column names referencing values in the referenced table.ref_type: The type of reference. Values can be"master"or"property".ref_table: The name of the referenced table.ref_cols: The list of columns corresponding tocols.on_delete: The action to perform when deleting a row. Values can be"deny","cascade","null"or"default".on_update: The action to perform when updating a row. Values can be"deny","cascade","null"or"default"
A foreign key relation can be of type
master or of type
property. Depending on the type of the foreign key relation, the DB Sync system
will reduce data rows differently when using data
filters:master: The relation between the referencing table and the referenced table is a detail->master relation. For example, when acclinestable (containing order lines) has accl_cusordcolumn referencing ids of acusord_idcolumn in acusordtable (containing order header information),cclinesis the detail table of thecusordmaster table.Regarding data filters:- If a master row is excluded by a data filter, all detail rows are excluded.
- If one of the detail rows is excluded by a data filter, the referenced master row will also be excluded.
-
property: The relation between the referencing table and the referenced table is an object->property relation. For example, when anemployeetable has anemp_shopcolumn referencing ids of ashop_idcolumn in ashoptable,employeeis considered as the object table, whileshopis the property table.Regarding data filters:- If a property row is excluded by a data filter, all referencing object rows will also be excluded.
- If an object row is excluded by a data filter, the referenced property row will not be excluded.
The next example showsn object->property table relation, in the
employee table definition, referencing the shop
property table:
{
"name": "fk_employee_shop",
"cols": [
"emp_shop"
],
"ref_type": "property",
"ref_table": "shop",
"ref_cols": [
"shop_id"
],
"on_delete": "deny",
"on_update": "deny"
}
Below is an example showing a detail->master table relation, in the
cclines table definition, referencing the cusord
master table: {
"name": "fk_cclines_cusord",
"cols": [
"ccl_cusord"
],
"ref_type": "master",
"ref_table": "cusord",
"ref_cols": [
"cusord_id"
],
"on_delete": "deny",
"on_update": "deny"
}