Masalah migration pada laravel

Halo! Saya ingin bertanya kenapa setiap saya mengikuti tutorial pada saat migrate selalu muncul error seperti ini? Saya menggunakan laravel 5.8 Error :

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_relation`.`#sql-1200_1e` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `passports` add constraint `passports_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

  at C:\xampp\htdocs\blog\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 `laravel_relation`.`#sql-1200_1e` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

Kode:

<?php

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

class CreatePassportsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('passports', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('no_pass', 100);
            $table->integer('user_id')->unsigned();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

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

avatar mmuqiit
@mmuqiit

32 Kontribusi 5 Poin

Diperbarui 4 tahun yang lalu

2 Jawaban:

biasanya tabel dari primary keynya ('users') belum ke migrate,coba bikin schema::table dibawah schema::create.bentuknya kurang lebih jadi gini

public function up() { Schema::create('passports', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('no_pass', 100); $table->integer('user_id')->unsigned(); $table->timestamps();

    });

   Schema:table(&#039;passports&#039;,function(Blueprint $table){
  

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

});

}
avatar Yfenanda
@Yfenanda

4 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

Tanggapan

Saya sudah ikuti cara ini, lalu saya migrate:fresh errornya masih sama

Jawaban Terpilih

Coba perhatikan pada file migrate user, cari baris berikut <pre> $table-&gt;increments('id'); //atau $table-&gt;bigIncrements('id'); </pre> kalau menggunakan increments biasa, kode di atas pada bagian <pre> $table-&gt;integer('user_id')-&gt;unsigned(); // Sudah cocok </pre> tetapi kalau menggunakan bigIncrements, coba diubah menjadi <pre> $table-&gt;bigInteger('user_id')-&gt;unsigned(); // atau bisa juga $table-&gt;unsignedBigInteger('user_id'); </pre> Semoga bisa membantu.

avatar zergz
@zergz

62 Kontribusi 47 Poin

Dipost 4 tahun yang lalu

Tanggapan

Terima kasih gan, Laravel memang udh update lumayan dari video tutorial yang ada di SK jadi harus menyesuaikan dlu hehe

Login untuk ikut Jawaban