gouseferoz
4 months ago

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

Posted 4 months ago by gouseferoz

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

Please sign in or create an account to participate in this conversation.