[英]Creating foreign key in codeigniter migration fails
我正在嘗試通過以下方式在codeigniter遷移中添加外鍵
我有兩張桌子
tbl_doc_types
id
name
tbl_docs
id
doc_type //foreign key to tbl_doc_types
所以在我的遷移中
class Migration_Add_Tbl_Legal_Documents extends CI_Migration
{
public function up()
{
$this->dbforge->add_field(array(
'doc_type' => array(
'type' => 'INT',
),
$this->dbforge->add_field('CONSTRAINT FOREIGN KEY (doc_type) REFERENCES tbl_doc_types(id)');
但是現在出現一個錯誤
Foreign key constraint is incorrectly formed")
我哪里錯了?
嘗試使用類似的東西
$this->db->query('ALTER TABLE `table` ADD CONSTRAINT `name_contraint` FOREIGN KEY(`id_table`) REFERENCES table2(`id_table2`) ON DELETE RESTRICT ON UPDATE RESTRICT;');
而不是$ this-> dbforge-> add_field
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.