Drupal currently does not make any use of foreign keys in the database (see #911352: Document that foreign keys may not be used by all drivers). DBTNG should provide methods for developers to create foreign keys if they want to use them.
Currently, foreign keys are defined in hook_schema() implementations like this:
'foreign keys' => [
'node_revision' => [
'table' => 'node_revision',
'columns' => [
'vid' => 'vid',
],
],
],
I suggest that a developer who wants the foreign key to be created would do it like this:
'foreign keys' => [
'node_revision' => [
'table' => 'node_revision',
'columns' => [
'vid' => 'vid',
],
'on update' => 'cascade',
'on delete' => 'set null',
],
],
If both 'on update' and 'on delete' are set to valid values, FKs would be created in the database, otherwise, they would not be. Valid values are: "restrict", "cascade", "set null", and "set default" ("no action" is valid in SQL but will not be supported for the reason given in #137).
Changes required for each database driver:
- createTableSql(), createKeysSql(), and _createKeys() methods need to create foreign keys, much like they create unique keys.
- addForeignKey(), dropForeignKey(), and foreignKeyExists() methods need to be added to the API.