【发布时间】:2018-04-05 07:09:51
【问题描述】:
我在 CI 中进行了迁移,现在我在其中创建一些外键时出错。我怎样才能制作一些外键,我在这里看到了一些与这个主题相关的问题,但它并没有解决我的问题。
这是我遇到错误的代码:
public function up() {
$this->dbforge->add_field(array(
'student_id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'course_id' => array(
'type' => 'INT',
'constraint' => 11,
),
'fname' => array(
'type' => 'VARCHAR',
'constraint' => 50,
),
'lname' => array (
'type' => 'VARCHAR',
'constraint' => 50,
),
'gender' => array (
'type' => 'VARCHAR',
'constraint' => 1
),
'bday' => array (
'type' => 'DATE'
)
));
$this->dbforge->add_key('student_id', TRUE);
$this->dbforge->add_field('CONSTRAINT FOREIGN KEY (course_id) REFERENCES tbl_course(id)');
$this->dbforge->create_table('tbl_students');
它给了我一个错误:
Can't create table `student_db`.`tbl_students` (errno: 150 "Foreign key constraint is incorrectly formed")
CONSTRAINT FOREIGN KEY (course_id) REFERENCES tbl_course(id),
CONSTRAINT `pk_tbl_students` PRIMARY KEY(`student_id`)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci
【问题讨论】:
-
检查外键列和引用列的类型或长度是否相同?
-
@Vickel 我需要使它们相同吗?感谢您的快速回复
-
@Vickel 你好,先生。是的。我检查了它,它是一样的。就这个。 'id' => 数组('type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE),
-
浏览这篇文章,你可能会找到你需要的:stackoverflow.com/questions/8434518/…
-
@Vickel 它已经是相同的数据类型了
标签: mysql codeigniter foreign-keys database-migration