JeffreyWay / Laravel-4-Generators

Rapidly speed up your Laravel workflow with generators
https://packagist.org/packages/way/generators
MIT License
27 stars 9 forks source link

Using same table for both sides of pivot creates invalid migration #463

Open cruciux opened 8 years ago

cruciux commented 8 years ago

I have the need to create a many-to-many on the same table (objects can be associated with a number of other objects).

The migration created tries to use the same foreign key:

public function up()
{
    Schema::create('munition_munition', function (Blueprint $table) {
        $table->integer('munition_id')->unsigned()->index();
        $table->foreign('munition_id')->references('id')->on('munitions')->onDelete('cascade');
        $table->integer('munition_id')->unsigned()->index();
        $table->foreign('munition_id')->references('id')->on('munitions')->onDelete('cascade');
        $table->primary(['munition_id', 'munition_id']);
    });
}

It should instead do something such as:

public function up()
{
    Schema::create('munition_munition', function (Blueprint $table) {
        $table->integer('munition_a_id')->unsigned()->index();
        $table->foreign('munition_a_id')->references('id')->on('munitions')->onDelete('cascade');
        $table->integer('munition_b_id')->unsigned()->index();
        $table->foreign('munition_b_id')->references('id')->on('munitions')->onDelete('cascade');
        $table->primary(['munition_a_id', 'munition_b_id']);
    });
}