Please post the module_data migration.
Apr 25, 2019
8
Level 2
"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
Level 4
@GOUSEFEROZ - Can you check if all your values on data_text.chapter column are valid values on module_data.id column?
Please or to participate in this conversation.