1
votes

i have 9 tables, when i run command :

php artisan migrate

only users table, migration table and password_reset table are created in my database. this is my sample code


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAlatsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('alats', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('merk_id')->unsigned();
            $table->integer('kategori_id')->unsigned();
            $table->integer('operator_id')->unsigned();
            $table->string('nama');
            $table->string('no_plat',15);
            $table->date('tahun');
            $table->string('volume',20);
            $table->text('keterangan');
            $table->enum('status',['ada','disewa','servis']);
            // $table->timestamp('created_at');
            // $table->timestamp('updated_at');
            $table->timestamps();
            $table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
            $table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
            $table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
        });
    }

    public function down()
    {
        Schema::dropIfExists('alats');
    }
}

please help me..?

4
any error from terminal??? or provide migration code sample.Parth Shah
this message on terminal : [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table db_sidewiratih.# sql-53c_8d (errno: 150 "Foreign key constraint is incorrectly formed") (SQ L: alter table alats add constraint alats_merk_id_foreign foreign key ( merk_id) references merks (id) on delete CASCADE) [PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table db_sidewiratih.# sql-53c_8d (errno: 150 "Foreign key constraint is incorrectly formed")Juliar Nasution
Can you please provide sample code of both migrations???Parth Shah
this is my code for alats table : pastebin.com/7zsTci9iJuliar Nasution

4 Answers

2
votes
Schema::create('alats', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('merk_id')->unsigned();
});

Schema::table('alats', function(Blueprint $table)
{
    $table->foreign('merk_id')
        ->references('merk_id')->on('merk_id_table_name')
        ->onDelete('cascade');
});

First create only table and then create foreign key. And make sure merk_id_table should migrate first.

2
votes

The migration files need to be migrated in right order. You can't migrate a table with a non existing foreign key.

You probably have a foreign key in some migration and the id key for that will come to existence later in the next migration file. That is why you get this error.

Check your migration files and watch out on order they get created.

For example if you have a foreign key alats_merk_id_foreign then the migration file with alats_merk_id must be migrated before.

1
votes

if you did every thing in all answers

I think your problem is from the foreign/primary key type and length

so you may create the id as integer and its corresponding foreign key as an integer with different length, or even as another type as string forexample

so try to make integers with unsigned and same length

try to use ->unsigned()->length(10)...

solution:

to make the pimary key and its corresponding foreign key in the same exact type and length

1
votes

Try doing it like this:


        Schema::disableForeignKeyConstraints();

        Schema::create('alats', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('merk_id')->unsigned();
            $table->integer('kategori_id')->unsigned();
            $table->integer('operator_id')->unsigned();
            $table->string('nama');
            $table->string('no_plat',15);
            $table->date('tahun');
            $table->string('volume',20);
            $table->text('keterangan');
            $table->enum('status',['ada','disewa','servis']);
            // $table->timestamp('created_at');
            // $table->timestamp('updated_at');
            $table->timestamps();
            $table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
            $table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
            $table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
        });

        Schema::enableForeignKeyConstraints();

You need to replicate the above for every migration.