Tahun baru, skill baru! 🚀. Masukkan kupon "skill2025" untuk diskon 30% di kelas apa saja

Gagal migrasi table one to one relationship pada laravel 5.8

saya ingin membuat Eloquent relationship one to one pada laravel tetapi terjadi masalah. disini saya ingin menguhubungkan antara id:siswa dengan id_siswa:telepon dan ketika saya migrate terjadi error seperti ini

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `db_siswakuapp`.`
telepon` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `telepon` add constraint `t
elepon_id_siswa_foreign` foreign key (`id_siswa`) references `siswa` (`id`) on delete cascade on update cascade)

  at G:\PHP PROGRAMMING\LARAVEL\P-Laravel\Querybuilder\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|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `db_siswakuapp`.`telepon` (errno: 15
0 "Foreign key constraint is incorrectly formed")")
      G:\PHP PROGRAMMING\LARAVEL\P-Laravel\Querybuilder\vendor\laravel\framework\src\Illuminate\Database\Connectio
n.php:458

  2   PDOStatement::execute()
      G:\PHP PROGRAMMING\LARAVEL\P-Laravel\Querybuilder\vendor\laravel\framework\src\Illuminate\Database\Connectio
n.php:458

ini kode table siswa

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableSiswa extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('siswa', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nisn', 4)->unique();
            $table->string('nama_siswa', 30);
            $table->date('tanggal_lahir');
            $table->enum('jenis_kelamin', ['L', 'P']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('siswa');
    }
}

dan ini kode table telepon yang tidak bisa dimigrate

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableTelepon extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('telepon', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->Integer('id_siswa')->primary('id_siswa');
            $table->string('telepon')->unique();
            $table->foreign('id_siswa')->references('id')->on('siswa')->onDelete('cascade')->onUpdate('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('telepon');
    }
}

avatar Sunset
@Sunset

18 Kontribusi 5 Poin

Dipost 5 tahun yang lalu

Tanggapan

coba pastikan dulu, yang dimigrate pertama kali table user baru table telepon

oke makasih kak sudah bisa

1 Jawaban:

Jawaban Terpilih

php artisan migrate:fresh sudah bisa

avatar Sunset
@Sunset

18 Kontribusi 5 Poin

Dipost 5 tahun yang lalu

Tanggapan

terimakasih sudah share

sama sama kak

Login untuk ikut Jawaban