我对模块的 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
..作为想法:也许,它还没有实现,我没有在 node
表中看到它(文档示例指向它的安装程序中的代码)。
感谢您的帮助。
最佳答案
根据 this post ,就在几个月前,hook_schema
函数还没有实现外键的创建。但是,它可以引用现有的。
也许解决方法是在 hook_init
(或其他 API 方法之一)中运行添加外键的 SQL。抱歉,目前似乎没有更好的解决方案。
关于drupal - Drupal 7 架构中的外键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4228857/