【发布时间】:2018-03-02 11:33:26
【问题描述】:
我是 Laravel 的新手。我之前创建了users 表。 employment 表已在迁移中创建。作为下一次迁移,我更改了users 表以将employment 表中的job_id 添加到users 表中。当我运行迁移时,它给出了上述错误。
注意:我需要将employment 表中的job_id 以job_id 的形式提供给users 表。 soumya 是我的数据库名称
当我在没有外键约束的情况下运行迁移时,它可以完美运行。
迁移:就业表
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无关,stackoverflow.com/…,一旦您在mysql中得到错误原因,在php端进行编辑将是小菜一碟。
-
尝试把这个
$table->integer('job_id')->after('deleted');改成这个$table->unsignedInteger('job_id')->after('deleted'); -
@Maraboc 它给出了完整性错误。
-
试试这个
$table->integer('job_id')->unsigned();!!
标签: php mysql laravel foreign-keys migration