2016-03-01 59 views
0

我試圖從products表中獲取price值並將其設置爲sold表。你能告訴我爲什麼我得到這個錯誤嗎? product_iduser_id工作正常。但我得到的錯誤,當我需要sold一般錯誤:1215無法添加外鍵約束

SOLD

Schema::create('sold', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users');  
     $table->integer('product_id')->unsigned(); 
     $table->foreign('product_id')->references('id')->on('products'); 
     $table->integer('price')->unsigned(); 
     $table->foreign('price')->references('price')->on('products'); 
     $table->integer('bid_price')->unsigned(); 
     $table->foreign('bid_price')->references('bid_price')->on('products');  
     $table->timestamps(); 
    }); 

PRODUCTS

Schema::create('products', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->string('slug'); 
     $table->string('title'); 
     $table->text('body'); 
     $table->integer('price'); 
     $table->integer('bid_price'); 
     $table->string('address'); 
     $table->string('condition'); 
     $table->integer('quantity'); 
     $table->string('img_1'); 
     $table->string('img_2'); 
     $table->string('img_3'); 
     $table->string('img_4'); 
     $table->integer('views'); 
     $table->timestamps(); 
    }); 

錯誤消息創建外price

General error: 1215 Cannot add foreign key constraint (SQL 
    : alter table `sold` add constraint sold_price_foreign foreign key (`price` 
) references `products` (`price`)) 

編輯

`LATEST FOREIGN KEY ERROR 
------------------------ 
2016-03-01 12:40:08 31a0 Error in foreign key constraint of table auction/#sql-2564_ef: 
foreign key (`price`) references `products` (`price`): 
Cannot find an index in the referenced table where the 
referenced columns appear as the first columns, or column types 
in the table and the referenced table do not match for constraint. 
Note that the internal storage type of ENUM and SET changed in 
tables created with >= InnoDB-4.1.12, and such columns in old tables 
cannot be referenced by such columns in new tables. 
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html 
for correct foreign key definition.` 

回答

0

檢查此鏈接:MySQL Cannot Add Foreign Key Constraint

第二個答案也說:

I had set one field as "Unsigned" and other one not. Once I set both columns to Unsigned it worked.

所以我會假設使您的產品列「價」無符號也應該解決問題。

像這樣:

Schema::create('products', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->integer('user_id')->unsigned(); 
    $table->foreign('user_id')->references('id')->on('users'); 
    $table->string('slug'); 
    $table->string('title'); 
    $table->text('body'); 
    $table->integer('price')->unsigned(); // <-- the difference is here! 
    $table->integer('bid_price'); 
    $table->string('address'); 
    $table->string('condition'); 
    $table->integer('quantity'); 
    $table->string('img_1'); 
    $table->string('img_2'); 
    $table->string('img_3'); 
    $table->string('img_4'); 
    $table->integer('views'); 
    $table->timestamps(); 
}); 

更新:

「最後FOREIGN KEY ERROR」 部分告訴你:

Cannot find an index in the referenced table

而且MySQL documentation說,關於外鍵:

MySQL requires indexes on foreign keys and referenced keys

所以基本上你必須在產品表中的價格列中添加一個索引。

我從來沒有與Laravel工作,但根據他們的文檔就應該像這樣工作:

$table->index('price'); 

更新2: 我只考慮這裏的技術方面,但看着一個數據庫結構在語義方面,創建一個外部關鍵字可能不是一個好主意,因爲價格並不是產品的標識符(具有相同價格的產品確實存在!)。

+0

謝謝,但它沒有奏效:/ – hulkatron

+0

你可以運行「SHOW ENGINE INNODB STATUS」查詢(如鏈接問題的接受答案所示),並告訴我們「最新的外鍵錯誤」部分? –

+0

我應該在我的數據庫sql中運行它嗎? – hulkatron

0

Cannot find an index in the referenced table

您的產品表需要您嘗試引用的列上的INDEX。我會說這必須是唯一的,否則MySQL無法確定它所引用的行。

+0

價格獨特?我認爲這是一個非常糟糕的決定 – hulkatron

+0

@hulkatron我認爲你不應該爲你的價格使用外鍵參考。外鍵用於引用數據行,而不是單獨的字段。您的錯誤指出它不能創建外鍵,因爲外部字段不是索引。 – thisisboris

+0

也許這是真的,也許我應該發佈物品時的價值 – hulkatron

相關問題