I'm trying to get price
value from products
table and set it to sold
table. Can u please tell me why I'm getting this error ? product_id
and user_id
works fine. But I get error when I need to create foreign price
in sold
table
SOLD
table
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
table
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();
});
Error message:
General error: 1215 Cannot add foreign key constraint (SQL
: alter table `sold` add constraint sold_price_foreign foreign key (`price`
) references `products` (`price`))
EDIT
`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.`