Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple PRIMARY keys #54

Open
martijn94 opened this issue Nov 21, 2017 · 0 comments
Open

Support for multiple PRIMARY keys #54

martijn94 opened this issue Nov 21, 2017 · 0 comments

Comments

@martijn94
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant