DBTNG should be able to create foreign keys

Created on 29 April 2014, over 11 years ago
Updated 31 July 2025, about 1 month ago

Drupal currently does not make any use of foreign keys in the database (see 🐛 Document that foreign keys may not be used by all drivers Fixed ). DBTNG should provide methods for developers to create foreign keys if they want to use them.

<!--break-->

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.
Feature request
Status

Needs work

Version

11.0 🔥

Component

database system

Created by

🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024