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

Wizurai1302's avatar

how i did fix

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (wesclic_blk.students, CONSTRAINT students_id_provinsi_foreign FOREIGN KEY (id_provinsi) REFERENCES users (id)) (SQL: update students set is_disable = 0, id_provinsi = 13, nama_provinsi = SUMATERA BARAT, id_kota = 1308, nama_kota = KABUPATEN LIMA PULUH KOTA, id_kecamatan = 1308020, nama_kecamatan = LUAK, id_kelurahan = 1308020001, nama_kelurahan = MUNGO, students.updated_at = 2023-08-08 10:38:15 where id = 1)

    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->nullable();
            $table->string('address')->nullable();
            $table->longtext('about')->nullable();
            $table->string('image')->nullable();
            $table->string('fb')->nullable();
            $table->string('tw')->nullable();
            $table->string('linked')->nullable();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                ->references('id')->on('users'); 
            $table->string('nik')->default();
            $table->date('tgl_lahir')->default(now());
            $table->string('dtks')->default();
            $table->boolean('is_disable')->default(0);
            $table->unsignedBigInteger('id_provinsi')->nullable();
            $table->foreign('id_provinsi')
            ->references('id')->on('users')
            ->nullable();
        $table->string('nama_provinsi')->nullable();
        $table->unsignedBigInteger('id_kota')->nullable();
        $table->foreign('id_kota')
            ->references('id')->on('users')
            ->nullable();
        $table->string('nama_kota')->nullable();
        $table->unsignedBigInteger('id_kecamatan')->nullable();
        $table->foreign('id_kecamatan')
            ->references('id')->on('users')
            ->nullable();
        $table->string('nama_kecamatan')->nullable();
        $table->unsignedBigInteger('id_kelurahan')->nullable();
        $table->foreign('id_kelurahan')
            ->references('id')->on('users')
            ->nullable();
        $table->string('nama_kelurahan')->nullable();
        $table->softDeletes();
        $table->timestamps();

        });
}
0 likes
14 replies
tisuchi's avatar

@wizurai1302 Adding a reference key may solve your issue.

Try to add this in your students table.

$table->unsignedBigInteger('province_id')->nullable();
$table->foreign('province_id')
    ->references('id')->on('provinces')
    ->nullable();
JussiMannisto's avatar

The issue is not with your migration. You're getting an error when trying to update a student model. Your id_provinsi column references the primary key on the users table. The foreign key constraint fails, so it looks like there's no user with ID 13.

Wizurai1302's avatar

@JussiMannisto this is the code that I have updated

              public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->nullable();
            $table->string('address')->nullable();
            $table->longtext('about')->nullable();
            $table->string('image')->nullable();
            $table->string('fb')->nullable();
            $table->string('tw')->nullable();
            $table->string('linked')->nullable();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                ->references('id')->on('users'); 
            $table->string('nik')->nullable();
            $table->date('tgl_lahir')->nullable();
            $table->string('dtks')->nullable();
            $table->boolean('is_disable')->nullable();
            $table->unsignedBigInteger('id_provinsi')->nullable();
            $table->foreign('id_provinsi')
                ->references('id')->on('provinsi')
                ->nullable();
            $table->string('nama_provinsi')->nullable();
            $table->unsignedBigInteger('id_kota')->nullable();
            $table->foreign('id_kota')
                ->references('id')->on('kota')
                ->nullable();
            $table->string('nama_kota')->nullable();
            $table->unsignedBigInteger('id_kecamatan')->nullable();
            $table->foreign('id_kecamatan')
                ->references('id')->on('kecamatan')
                ->nullable();
            $table->string('nama_kecamatan')->nullable();
            $table->unsignedBigInteger('id_kelurahan')->nullable();
            $table->foreign('id_kelurahan')
                ->references('id')->on('kelurahan')
                ->nullable();
            $table->string('nama_kelurahan')->nullable();
            // $table->foreign('id_user')->references('id')->on('users');
            $table->softDeletes();
            $table->timestamps();

        });
}

and this display on the geographic table

public function up()
{
    Schema::create('provinsi', function (Blueprint $table) {
        $table->id();
        $table->string('name')->default();
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('kota', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('kecamatan', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('kelurahan', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
    });
}
Snapey's avatar

Your original message had

FOREIGN KEY (id_provinsi) REFERENCES users (id))

but you say you have fixed this by correcting the name in the migration

So how can you possibly have the SAME error?

1 like
JussiMannisto's avatar

@Wizurai1302 If you're getting the exact same error, then you obviously haven't run the migrations.

Do you understand how migrations work? If you edit the migration file after running it, then running "php artisan migrate" won't do anything since the migration has already been handled. You need to either roll back the previous migration or use migrate:fresh which clears your DB and runs all migrations from scratch. If you're confused, I recommend you read Laravel's documentation on migrations.

Lara_Love's avatar

The error message you provided indicates an integrity constraint violation in an SQL query. Specifically, it states that there is a foreign key constraint failure when trying to add or update a child row in the "students" table.

The foreign key constraint "students_id_provinsi_foreign" references the "id" column in the "users" table. The error suggests that the foreign key constraint is not being satisfied, meaning that the value being inserted or updated in the "id_provinsi" column of the "students" table does not exist in the referenced "id" column of the "users" table.

To resolve this issue, you need to ensure that the value being assigned to the "id_provinsi" column in the "students" table corresponds to an existing value in the "id" column of the "users" table. Double-check the data being used in the query and verify that the referenced foreign key values are correct and exist in the referenced table.

Please or to participate in this conversation.