我正在尝试使用这样的外键创建表(使用迁移)
public function safeUp()
{
$this->createTable('tbl_category', array(
'id'=>'pk',
'title'=>'string NOT NULL',
'url'=>'string NOT NULL UNIQUE'
));
$this->addForeignKey('FK_category', 'tbl_product', 'category_id', 'tbl_category', 'id', 'CASCADE', 'NO ACTION');
}
它在 MySQL 中完美运行,但现在我想使用 SQLite,这段代码给出了一个错误,在 SQLite 中我无法向现有表添加外键,所以我查看了createTable
方法的定义:
public integer createTable(string $table, array $columns, string $options=NULL)
并尝试使用$options
param 在此处添加我的外键,但它会生成以下内容:
CREATE TABLE 'tbl_category' (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"title" varchar(255) NOT NULL,
"url" varchar(255) NOT NULL UNIQUE
)
CONSTRAINT FK_category
FOREIGN KEY tbl_product(category_id)
REFERENCES tbl_category(id)
ON DELETE CASCADE ON UPDATE NO ACTION
显然,“CONSTRAINT ...”代码应该在这些括号内,但事实并非如此。那么如何创建这个外键呢?