使用命令行迁移时,我收到此错误...
(errno: 150 "Foreign key con straint is incorrectly formed")
这是我的迁移代码..
public function up()
{
Schema::enableForeignKeyConstraints();
Schema::create('student', function (Blueprint $table) {
$table->integer('student_id')->unsigned()->primary();
$table->string('student_name', 200);
$table->string('student_email', 50);
$table->integer('class_id')->unsigned()->nullable();
$table->string('password', 100);
$table->rememberToken();
$table->timestamps();
});
Schema::table('student', function($table) {
$table->foreign('class_id')->references('class_id')->on('class_tbl');
});
}
我还没有找到解决方法,我无法弄清楚出了什么问题......
编辑:
以下是class_tbl的迁移...
public function up()
{
Schema::enableForeignKeyConstraints();
Schema::create('class_tbl', function (Blueprint $table) {
$table->integer('class_id')->unsigned()->primary();
$table->string('course', 10);
$table->string('section', 5);
$table->string('school_year', 10);
$table->integer('prof_id')->unsigned();
});
Schema::table('class_tbl', function($table) {
$table->foreign('prof_id')->references('prof_id')->on('professor');
});
}
答案 0 :(得分:0)
嗯...一个可以为空的外键?试试这个......
Schema::create('student', function (Blueprint $table) {
$table->integer('student_id')->unsigned()->primary();
$table->string('student_name', 200);
$table->string('student_email', 50);
$table->integer('class_id')->unsigned();
$table->foreign('class_id')->references('class_id')->on('class_tbl');
$table->string('password', 100);
$table->rememberToken();
$table->timestamps();
});
Schema::create('class_tbl', function (Blueprint $table) {
$table->integer('class_id')->unsigned()->primary();
$table->string('course', 10);
$table->string('section', 5);
$table->string('school_year', 10);
$table->integer('prof_id')->unsigned();
$table->foreign('prof_id')->references('prof_id')->on('professor');
});