2016-09-07 34 views
1

Laravel 5不会在我的迁移文件上创建外键: 我跟随此处的文档 https://laravel.com/docs/5.3/blade,它不起作用。创建表时没有限制将不会创建Laravel 5外键

<?php 

use Carbon\Carbon; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class Comments extends Migration 
{ 
/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('comments',function($newTable){ 
     $newTable->increments('id'); 
     $newTable->integer('painting_id')->length(10)->unsigned(); 
     $newTable->integer('user_id'); 
     $newTable->text('comment'); 
     $newTable->date('crated')->default(Carbon::now()); 
     $newTable->timestamps(); 
    }); 

    Schema::table('comments',function($table){ 
     $table->foreign('painting_id')->references('id')->on('paintings')->onDelete('cascade'); 
    }); 


} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::drop('comments'); 
} 
} 

回答

0

尝试将表设置为Innodb引擎。 在表格创建结束时添加此项。

Schema::create('comments',function($newTable){ 
//The other fields 
    $newTable->engine = "InnoDB";   
});