2016-11-24 207 views
1

我尝试迁移在Laravel两个表Laravel迁移 - 不能添加外键

表 'Aitems'

<?php 

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

    class CreateAitemsTable extends Migration 
    { 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // 
    Schema::create('aitems', function($table) { 

     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->integer('item_id'); 
     $table->integer('listing_id');   
     $table->string('item_category'); 
     $table->string('item_name'); 
     $table->string('item_price'); 
     $table->string('item_description'); 
     $table->string('item_storing_address_1'); 
     $table->string('item_storing _address_2'); 
     $table->string('item_storing_suburb'); 
     $table->string('item_addr_long'); 
     $table->string('item_addr_lang'); 
     $table->timestamps();    
    }); 

    Schema::table('aitems', function($table){ 
     $table->foreign('item_id')->references('item_id')->on('itemfeatures'); 
     $table->foreign('listing_id')->references('listing_id')->on('itemfeatures');  
    }); 

} 

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

表 'itemfeatures'

<?php 

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

class CreateItemfeaturesTable extends Migration 
{ 
/** 
    * Run the migrations. 
    * 
    * @return void 
*/ 
public function up() 
{ 
    // 
    Schema::create('itemfeatures', function(Blueprint $table) { 

     $table->engine = 'InnoDB';  

     $table->increments('id');   
     $table->integer('listing_id')->unsigned();    
     $table->integer('item_id')->unsigned(); 
     $table->string('feature_id'); 
     $table->string('feature_description');   
     $table->timestamps(); 

    }); 
} 

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

}

尝试执行迁移时,收到'1215错误无法添加外键约束'的消息。已经看过很多其他论坛,仍然无法弄清楚我做错了什么。非常感谢那里的任何帮助。谢谢。

回答

0

您的表格结构看起来很奇怪,但如果您知道您在做什么,请修复aitems表中的列。更改此:

$table->integer('item_id'); 
$table->integer('listing_id'); 

要这样:

$table->integer('item_id')->unsigned(); 
$table->integer('listing_id')->unsigned();