4

I am trying to create 2 tables, linked together, using this code:

$schema['parent'] = array(
    "fields" => array(
        "id" => array(
            'description' => 'The primary identifier',
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
        ),
        'data' => array(
            'description' => 'The stored data.',
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
            'default' => '',
        ),
    ),
    'primary key' => array('id'),
);

$schema['child'] = array(
    // {{{fields

    "fields" => array(
        "id" => array(
            'description' => 'The primary identifier',
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
        ),
        "parent_id" => array(
            'type' => 'int',
            'unsigned' => TRUE,
        ),
        'data' => array(
            'description' => 'The stored data.',
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
            'default' => '',
        ),
    ),
    // }}}
    'indexes' => array(
        "parent_id" => array("parent_id"),
    ),
    'foreign keys' => array(
        'parent_id' => array(
            'table' => 'parent',
            'columns' => array('id' => 'parent_id'),
        ),
    ),
    'primary key' => array('id'),
);

foreach ($schema as $key => $value) {
    print $key . "\n";
    db_create_table($key, $value);
}

The tables are created, but the foreign keys are not. Using MySQL/InnoDB. How to fix it?

Also how to set ON DELETE CASCADE and ON UPDATE CASCADE?

1 Answer 1

8

Schema Reference

Foreign key definitions were added in Drupal 7 for documentation purposes only, and do not modify the database.

1
  • 1
    which explains, perhaps, why the D7 spec does not allow for ON DELETE CASCADE etc. Guess I'll just resort to command-line mysql to fix this hole. Nov 19, 2012 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.