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

shariff's avatar
Level 51

Foreign key constraint not working

I am new to Laravel while adding a new column to the existing table it is giving me the error.

Illuminate\Database\QueryException : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (schoolmanagement.#sql-314_2c, CONSTRAINT students_parentsid_foreign FOREIGN KEY (parentsid) REFERENCES parent_names (id) ON DELETE CASCADE) (SQL: alter table students add constraint students_parentsid_foreign foreign key (parentsid) references parent_names (id) on delete cascade)

My model

Schema::table('students', function (Blueprint $table) {

        $table->unsignedBigInteger('parentsid')->after('id');
        $table->foreign('parentsid')->references('id')->on('parent_names')->onDelete('cascade');

        //
    });

public function down() { Schema::table('students', function (Blueprint $table) { // }); }

0 likes
9 replies
bobbybouwmann's avatar

You need to make sure that the parent_names table exists, otherwise you can't make the connection between the tables using a foreign key!

Snapey's avatar
Snapey
Best Answer
Level 122

I assume you are adding this in a migration which runs before the parents table is created

move your fk definitions to the parents table migration or to a migration of their own

Snapey's avatar

really?

List your migrations in the order they are performed

shariff's avatar
Level 51

yes, I am doing in migration. when I run PHP artisan migrate command it is giving me below error

Migrating: 2019_08_17_070731_add_foreign_key

Illuminate\Database\QueryException : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (schoolmanagement.#sql-314_2c, CONSTRAINT students_parentsid_foreign FOREIGN KEY (parentsid) REFERENCES parent_names (id) ON DELETE CASCADE) (SQL: alter table students add constraint students_parentsid_foreign foreign key (parentsid) references parent_names (id) on delete cascade)

at C:\xampp\htdocs\schoolmanagement\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {

664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| }

shariff's avatar
Level 51

2014_1012000000create_users_table.php 2014_1012_100000create_password_resets_table.php 201908_14_105904_create_permission_tables.php 201908_14_114112_create_principals_table.php 201908_16_124145_create_parent_names_table.php 2019_08_17_070731_add_foreign_key.php

Snapey's avatar

Where do you create students table?

shariff's avatar
Level 51

After migrating I have deleted the migration file of students table

Please or to participate in this conversation.