[英]How to create composite key in Laravel?
I tried many ways but nothing work.我尝试了很多方法,但没有任何效果。 I need to add a composite key to my table daily_deals_products.我需要在我的表 daily_deals_products 中添加一个复合键。 Which is developed in laravel and mysql.这是在laravel和mysql中开发的。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDailyDealsProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('daily__deals__products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('daily__deals__products');
}
}
try this :尝试这个 :
$table->primary(['id','product_id']);
public function up()
{
Schema::create('daily__deals__products', function (Blueprint $table) {
$table->primary(['id', 'product_id']);
$table->unsignedInteger('id');
$table->foreignId('product_id')->nullable()->constrained()->onDelete('cascade');
}
Schema::create("daily__deals__products", function($table) {
$table->increments('id');
$table->integer('product_id');
$table->string('name');
});
DB::unprepared('ALTER TABLE `daily__deals__products` DROP PRIMARY KEY, ADD PRIMARY KEY ( `id` , `product_id` )');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.