3

I have a trouble with Drupal 7 schema for module. There are 4 tables but for sample 2 will be enough:

function mymodule_schema() {
$schema['series'] = array(
    'fields' => array(
        'id' => array(
            'type' => 'serial',
            'unsigned' => true,
            'not null' => true,
        ),
        'name' => array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => true,
        ),
    ),
    'unique keys' => array(
        'name' => array('name'),
    ),
    'primary key' => array('id'),
);

$schema['sermon'] = array(
    'fields' => array(
        'id' => array(
            'type' => 'serial',
            'unsigned' => true,
            'not null' => true,
        ),
        'title' => array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => true,
        ),
        'series_id' => array(
            'type' => 'int',
        ),
    ),
    'foreign keys' => array(
        'series_id' => array(
            'table' => 'series',
            'columns' => array('series_id' => 'id'),
        ),
    ),
    'primary key' => array('id'),
);
return $schema;
}

This code create tables but not foreign keys. Example for implementation I get from Drupal.org: http://drupal.org/node/146939

Drupal version is 7.0-beta 3 ..As idea: maybe, it isn't implemented yet, I don't see it in node table (documentation example point to code from it's installer).

Thank for your help.

apaderno
  • 28,547
  • 16
  • 75
  • 90
lifecoder
  • 33
  • 1
  • 3
  • The only thing I can think of is that perhaps `id` cannot be uniquely identified in the query. You could change sermon.id to `sermon_id` and series.id to `series_id` or something. Now you've got me curious, let me know it that works! :) – Courtney Christensen Nov 19 '10 at 20:08
  • zourtney, I'll try yesterday (it's night for me now), but even if it's a problem (that strange, table+fieldname should be enough and raw SQL statement do this work correct) I need solution for right this names because schema is provided by third party and I simply couldn't change it on production. – lifecoder Nov 19 '10 at 20:38
  • Ok, forget that idea; I guess this isn't actually implemented it Drupal yet. I made a post below. I remember running into this issue myself a while ago. I would seem I mis-remembered the "solution". – Courtney Christensen Nov 19 '10 at 23:38

3 Answers3

5

According to this post, just a few months ago, the hook_schema function does not implement the creation of foreign keys. It can, however, reference existing ones.

Perhaps a work-around would be to run the foreign-key-adding SQL in hook_init (or one of the other API methods). Sorry, there doesn't seem to be a better resolution to this right now.

Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
2

You have the right idea, this is not implemented yet :/

I have no idea why they do not state this prominently, but apparently, the foreign key declarations are for documentation purposes only at the moment (and a preparation to do something useful with them later, at least one would hope so).

See this comment and below on the (misnamed) 'add foreign keys to core' thread.

Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
0

this is a very old post, but for reference this can help

'foreign keys' => array(
    'series_id' => array(
        'table' => 'series',
        'columns' => array('id' => 'series_id'),
    ),
),
sona
  • 96
  • 2
  • 14