gpt4 book ai didi

mysql - codeigniter迁移错误外键约束

转载 作者:行者123 更新时间:2023-11-30 21:51:48 24 4
gpt4 key购买 nike

我在我的 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

最佳答案

你需要运行

dbforge->add_key    

之前

dbforge->create_table

像这样:

$this->dbforge->add_field('CONSTRAINT FOREIGN KEY (id) REFERENCES table(id)');

$this->dbforge->add_column('table',[
'CONSTRAINT fk_id FOREIGN KEY(id) REFERENCES table(id)',
]);

关于mysql - codeigniter迁移错误外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46911330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com