我在使用 Drupal 7 模块架构时遇到问题。有 4 个表,但对于示例 2 来说就足够了:

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;
}

此代码创建表但不创建外键。我从 Drupal.org 获得的实现示例: http://drupal.org/node/146939

Drupal版本是7.0-beta 3 .. AS IDEA:也许,它还没有实现,我没有看到它 node 表(文档示例指向其安装程序中的代码)。

感谢您的帮助。

有帮助吗?

解决方案

根据这个帖子,就在几个月前,hook_schema功能没有实现的创造外键。它可以,但是,参考现有的。

也许一个变通将运行在hook_init外键-添加SQL(或另一API方法之一)。对不起,似乎没有成为一个更好的解决这个现在。

其他提示

你的想法是对的,但这还没有实现:/

我不知道他们为什么不突出地说明这一点,但显然,外键声明目前仅用于文档目的(并准备稍后对它们做一些有用的事情,至少有人希望如此)。

此评论及以下评论 关于(错误命名) '将外键添加到核心' 线。

这是一个非常旧的文章,但对于参考这可以帮助

'foreign keys' => array(
    'series_id' => array(
        'table' => 'series',
        'columns' => array('id' => 'series_id'),
    ),
),
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top