错误:外键约束违规。无法添加/更新子行

时间:2017-12-04 17:39:33

标签: mysql database foreign-keys laravel-5.2

任何人都可以帮助我吗?我被困在设置外键约束。 我正在

 ERROR:foreign key constraint violation: Can't add/update a child row. 

我有一个用户,项目,小时,任务和子任务表。 我已按此顺序创建了表的迁移

 1.Users 2.Projects 3.Hours 4.Tasks 5.Sub-tasks

在这些迁移之后,我创建了一个新的迁移,其中我在小时表3字段中添加了五个新字段,从任务和子任务表i-e task_id和subtask_id添加了两个外键。 我已经使外键unsigned(),这些外键与父表相同。

我已经搜索了很多,但大多数回复说你在父表或类似的东西之前创建子表。

小时迁移

class CreateHoursTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
      Schema::create('hours', function (Blueprint $table) {
        $table->increments('id');
        $table->string('actual_hours');
        $table->string('productive_hours');
        $table->integer('project_id')->unsigned();
        $table->foreign('project_id')
         ->references('id')
         ->on('projects')
         ->onDelete('cascade');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
   Schema::drop('hours');
}
}

任务迁移:

class CreateTasksTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');

        /* Relations Table Field Starts */
        $table->integer('project_id')->unsigned();
        $table->foreign('project_id')
            ->references('id')
            ->on('projects')
            ->onDelete('cascade');

        /* Relations Table Field Ends */

        $table->string('name');
        $table->string('key');
        $table->text('description');

        $table->integer('percentDone');
        $table->date('duedate');
        $table->integer('reporter');
        $table->integer('follower');
        $table->string('tags');

        /*Task ENUM Fields Starts*/
        $table->enum('types', ['New Feature','Bug','Improvement','Task']);
        $table->enum('priority', ['Blocker','Minor','Major','Trivial','Critical']);
        $table->enum('workflow', ['Todo','In Progress','In QA','Completed']);
        $table->enum('component', ['Web','Android','IOS']);
        /*Task ENUM Fields Starts*/

        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('tasks');
}
}

子任务迁移:

class CreateSubtasksTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('subtasks', function (Blueprint $table) {
        $table->increments('id');

        /* Relations Table Field */
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->integer('task_id')->unsigned();
        $table->foreign('task_id')
            ->references('id')
            ->on('tasks')
            ->onDelete('cascade');

        $table->string('name');
        $table->text('description');
        $table->integer('percentDone');
        $table->date('duedate');
        $table->string('reporter');
        $table->string('follower');
        $table->string('tags');
        $table->enum('priority', ['Blocker','Minor','Major','Trivial','Critical']);
        $table->enum('Workflow', ['Todo','In Progress','In QA','Completed']);
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('subtasks');
}
}

迁移以在小时表中添加新字段:

class AlterTableHoursAddColumnsHours extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('hours', function (Blueprint $table) {
        $table->integer('estimated_hours');
        $table->integer('internal_hours');
        $table->integer('consumed_hours');
        /*Now Fields for Task And Subtask Tables Starts*/
        $table->integer('task_id')->unsigned();
        $table->foreign('task_id')
            ->references('id')
            ->on('tasks')
            ->onDelete('cascade');

        $table->integer('subtask_id')->unsigned();
        $table->foreign('subtask_id')
            ->references('id')
            ->on('subtasks')
            ->onDelete('cascade');
        /*Now Fields for Task And Subtask Tables Ends*/
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('hours', function (Blueprint $table) {
        $table->dropColumn('estimated_hours');
        $table->dropColumn('internal_hours');
        $table->dropColumn('consumed_hours');

        $table->dropForeign('hours_task_id_foreign');
        $table->dropColumn('task_id');

        $table->dropForeign('hours_subtask_id_foreign');
        $table->dropColumn('subtask_id');
    });
}
}

这是因为我在任务和子任务表之前创建了小时表。 ???任何解决方案任何帮助??

0 个答案:

没有答案