mysql-workbench-export-laravel-5-migrations icon indicating copy to clipboard operation
mysql-workbench-export-laravel-5-migrations copied to clipboard

Support for multiple PRIMARY keys

Open martijn94 opened this issue 7 years ago • 0 comments

Hi,

First of all thanks for making this plugin, saved me so much hours already!

I found this issue where a PRIMARY key with multiple index collumns would not export correctly to a migration.

I have to following Index in Workbench: screen shot 2017-11-21 at 16 02 32

This results in the following migration:

<?php

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

class CreateUpgradesClubsTable extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'upgrades_clubs';

    /**
     * Run the migrations.
     * @table upgrades_clubs
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable($this->set_schema_table)) return;
        Schema::create($this->set_schema_table, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('upgrade_id');
            $table->unsignedInteger('club_id');

            $table->index(["upgrade_id"], 'fk_upgrades_has_clubs_upgrades1_idx');

            $table->index(["club_id"], 'fk_upgrades_has_clubs_clubs1_idx');


            $table->foreign('upgrade_id', 'fk_upgrades_has_clubs_upgrades1_idx')
                ->references('id')->on('upgrades')
                ->onDelete('cascade')
                ->onUpdate('no action');

            $table->foreign('club_id', 'fk_upgrades_has_clubs_clubs1_idx')
                ->references('id')->on('clubs')
                ->onDelete('cascade')
                ->onUpdate('no action');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->set_schema_table);
     }
}

However this should result in the following migration:

<?php

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

class CreateUpgradesClubsTable extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'upgrades_clubs';

    /**
     * Run the migrations.
     * @table upgrades_clubs
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable($this->set_schema_table)) return;
        Schema::create($this->set_schema_table, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->unsignedInteger('upgrade_id');
            $table->unsignedInteger('club_id');

            $table->primary(['upgrade_id', 'club_id']);
            
            $table->index(["upgrade_id"], 'fk_upgrades_has_clubs_upgrades1_idx');

            $table->index(["club_id"], 'fk_upgrades_has_clubs_clubs1_idx');


            $table->foreign('upgrade_id', 'fk_upgrades_has_clubs_upgrades1_idx')
                ->references('id')->on('upgrades')
                ->onDelete('cascade')
                ->onUpdate('no action');

            $table->foreign('club_id', 'fk_upgrades_has_clubs_clubs1_idx')
                ->references('id')->on('clubs')
                ->onDelete('cascade')
                ->onUpdate('no action');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->set_schema_table);
     }
}

Thanks in advance!

martijn94 avatar Nov 21 '17 15:11 martijn94