我是Laravel的新手。我以前创建了users
表。 employment
表已在迁移中创建。作为下一次迁移我已经改变users
表添加job_id
在employment
表到users
表。当我运行迁移时,会出现上述错误。(错误:150“外键约束不正确”)
注意:我需要在employment
表中给job_id
表users
表作为job_id
。 soumya是我的数据库名称
当我运行没有外键约束的迁移时,它可以很好地工作。
Migations:就业表
public function up()
{
Schema::create('employment', function (Blueprint $table) {
$table->increments('job_id');
$table->string('job_title');
$table->string('job_description')->nullable()->default(NULL);
$table->string('slug')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::drop('employment');
}
迁移改变users
表
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('job_id')->after('deleted');
$table->foreign('job_id')->references('job_id')->on('employment');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_job_id_foreign');
$table->dropColumn('job_id');
});
}
该错误纯粹与mysql有关,不是php也不是laravel,一旦你在mysql中得到了错误原因在PHP方面编辑将是一个和平的蛋糕。 – hassan
[迁移:无法在laravel中添加外键约束]可能的重复(https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel) – hassan
尝试更改此' ('deleted');'到'this'$ table-> unsignedInteger('job_id') - > after('deleted');' – Maraboc