Document that foreign keys may not be used by all drivers

Created on 14 September 2010, over 14 years ago
Updated 8 February 2023, almost 2 years ago

I am using 'mysql Ver 14.12 Distrib 5.0.70, for pc-linux-gnu (x86_64) using readline 5.2'.

Trying to create a foreign key does not seem to be working. The code executes with no errors and the table is constructed but the foreign key does not exist. Replacing a valid lookup table name with garbage also does not throw an error and the table is still constructed leaving me to believe that it is simply ignoring this altogether.

From the MySQL docs (http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-foreign-keys.html):
For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it. In the future, the implementation will be extended to store this information in the table specification file so that it may be retrieved by mysqldump and ODBC. At a later stage, foreign key constraints will be implemented for MyISAM tables as well.

My tables are set to be InnoDB by default and not MyISAM so the foreign key constraint should not be ignored.

  $schema['lookup_table'] = array(
    'description' => 'This is the table that will contain the lookup data.',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for lookup data.',
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => ''),
      'name' => array(
        'description' => 'Name of lookup item.',
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
        'default' => ''),
    ),
    'primary key' => array('id'),
  );
  $schema['main_table'] = array(
    'description' => 'This is the table that will reference the lookup table.',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for main table.',
        'type' => 'varchar',
        'length' => 9,
        'not null' => TRUE,
        'default' => ''),
      'lookup_id' => array(
        'description' => 'ID of lookup item.',
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => ''),
    ),
    'foreign keys' => array(
      'lookup_table_foreign_key' => array(
        'table' => 'lookup_table',
        'columns' => array('lookup_id' => 'id'),
      ),
    ),
    'primary key' => array('id'),
  );

I've tried to reverse the field names in the columns entry as I could not tell from the documentation (http://drupal.org/node/146939) which is to come first but that did not help either.

πŸ› Bug report
Status

Fixed

Version

7.0 ⚰️

Component
DatabaseΒ  β†’

Last updated 2 days ago

  • Maintained by
  • πŸ‡³πŸ‡±Netherlands @daffie
Created by

πŸ‡ΊπŸ‡ΈUnited States portigui

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