dimanche 29 mai 2016

Laravel migrations: create referenced table before making a reference to it

Here is my migration method:

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('item_type_id')->unsigned();
        $table->integer('character_class');
        $table->integer('character_race');
        $table->integer('required_level');
        $table->integer('quality');
        $table->integer('durability');
        $table->integer('buy_price');
        $table->integer('sell_price');
        $table->timestamps();
    });

    Schema::table('items', function($table) {
        $table->foreign('item_type_id')->references('id')->on('item_types')->onDelete('cascade');
    });
}

The problem is that migration for the item_types table is after items migration. So there is no item_types table while creating items table, then migration will fail at creating foreign key. Is there a way to delay foreign constraints and run them after table creations? Or I have to separate the foreign constraints to another migration?! Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire