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

Randy_Johnson's avatar

Migration Foreign Key Error

    public function up()
    {
        Schema::create('injuries', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->String('description');
            $table->timestamps();
            
        });
    }

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`school`.`injuries`, CONSTRAINT `injuries_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `injuries` (`user_id`, `description`, `updated_at`, `created_at`) values (6, quia, 2020-10-13 15:47:11, 2020-10-13 15:47:11))
0 likes
7 replies
Sinnbeck's avatar

Is the users migration before this one? The users table must exist when this is created. If it is, please post the users migration

Randy_Johnson's avatar
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (123.32ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (81.87ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (101.33ms)
Migrating: 2020_10_10_204425_create_permission_tables
Migrated:  2020_10_10_204425_create_permission_tables (1,656.44ms)
Migrating: 2020_10_11_080944_create_injuries_table
Migrated:  2020_10_11_080944_create_injuries_table (232.58ms)
Migrating: 2020_10_11_080959_create_payments_table
Migrated:  2020_10_11_080959_create_payments_table (59.41ms)
Migrating: 2020_10_11_081006_create_reports_table
Migrated:  2020_10_11_081006_create_reports_table (56.84ms)
Migrating: 2020_10_11_081015_create_fees_table
Migrated:  2020_10_11_081015_create_fees_table (78.01ms)
Migrating: 2020_10_11_081022_create_grades_table
Migrated:  2020_10_11_081022_create_grades_table (57.43ms)
Migrating: 2020_10_11_081029_create_time_tables_table
Migrated:  2020_10_11_081029_create_time_tables_table (77.89ms)
Migrating: 2020_10_11_081118_create_subjects_table
Migrated:  2020_10_11_081118_create_subjects_table (77.86ms)
Randy_Johnson's avatar

Okay, it works just as long as I don't seed.

Yes yes, I see the problem now.

Edit: When I turn on the seeds it won't allow, the users is above as a seed.

        \App\Models\User::factory(20)->create();
        \App\Models\Fee::factory(20)->create();
        \App\Models\Grade::factory(5)->create();
        \App\Models\Injury::factory(30)->create();
        \App\Models\Payment::factory(15)->create();
        \App\Models\Report::factory(30)->create();
        \App\Models\Subject::factory(5)->create();
        \App\Models\TimeTable::factory(20)->create();
Sinnbeck's avatar

Sorry I am confused. Was it solved? Or are you getting a new error?

Please or to participate in this conversation.