I have a Laravel pivot table which contains multiple primary keys: "order_id", "product_id", "variation_id".
I need to add a new column called "id" to this table which I do as follows:
public function up()
{
Schema::table('order_product', function (Blueprint $table)
{
$table->integer('id')->unsigned()->after('variation_sku');
});
}
My original migration file looks as follows:
public function up()
{
Schema::create('order_product', function (Blueprint $table)
{
$table->integer('order_id')->unsigned()->index();
$table->foreign('order_id')->references('order_id')->on('orders')->onDelete('cascade');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('product_id')->on('products')->onDelete('cascade');
$table->integer('variation_id')->unsigned()->index()->nullable();
$table->jsonb('variation')->nullable();
$table->integer('qty');
$table->string('variation_status')->default('bakery');
$table->timestamps();
$table->softDeletes();
$table->primary(['order_id', 'product_id', 'variation_id']);
});
}
This migration includes my 3 composite keys.
I now need to add the new column "id" and add this column to the composite keys.
I cannot find anything in the Laravel documentation and have a suspicion that this may not be possible as it may affect past records in this table.
You could drop the primary key then create a new one:
public function up()
{
Schema::table('order_product', function (Blueprint $table)
{
$table->integer('id')->unsigned()->after('variation_sku');
$table->dropPrimary();
$table->primary(['id', 'order_id', 'product_id', 'variation_id']);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.