我試圖從products
表中獲取price
值並將其設置爲sold
表。你能告訴我爲什麼我得到這個錯誤嗎? product_id
和user_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.`
謝謝,但它沒有奏效:/ – hulkatron
你可以運行「SHOW ENGINE INNODB STATUS」查詢(如鏈接問題的接受答案所示),並告訴我們「最新的外鍵錯誤」部分? –
我應該在我的數據庫sql中運行它嗎? – hulkatron