Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gouseferoz's avatar

"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

I have a table without any foreign key in it and i want to add a foreign key to have onDelete('cascade') between two tables.

public function up()
    {
        Schema::create('data_text', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
        $table->unsignedInteger('chapter');
            $table->string('data')->nullable();    
            $table->timestamps();
    });
    }

and my new migration is

public function up()
    {
        Schema::table('data_text', function (Blueprint $table) {
            $table->foreign('chapter')->references('id')->on('module_data')->onDelete('cascade');
        });

    }

while migrating, i am getting the following error

Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`in
stacks`.`#sql-d0_13b`, CONSTRAINT `data_text_chapter_foreign` FOREIGN KEY (`chapter`) REFERENCES `module_data` (`id`) ON DELETE CASCADE)")
      C:\xampp\htdocs\Instacks\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

  2   PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`instacks`.`#sql-d0_13b`
, CONSTRAINT `data_text_chapter_foreign` FOREIGN KEY (`chapter`) REFERENCES `module_data` (`id`) ON DELETE CASCADE)")
      C:\xampp\htdocs\Instacks\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:117

can anyone help me whats issue happening here?

Regards, Feroz

0 likes
8 replies
gouseferoz's avatar

@staudenmeir

Module Data Migration:

public function up()
    {
        Schema::create('module_data', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('module_id');
            $table->timestamps();
            $table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
        });
    }

Modules Migration:

public function up()
    {
        Schema::create('modules', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('user_id');
            $table->timestamps();
        });
    }
staudenmeir's avatar

Does the data_text table already contain data when you add the foreign key?

mkbat's avatar

You are changing an existent field. You need to add change(). Without this, your migration is adding a new field that already exists.

public function up()
    {
        Schema::table('data_text', function (Blueprint $table) {
            $table->foreign('chapter')->references('id')->on('module_data')->onDelete('cascade')->change();
        });

    }
gouseferoz's avatar

@MKBAT - That solves the problem.

I had few rows of data in data_text which has the module_data.id

and that id is not present in module_data.

1 like

Please or to participate in this conversation.