Php Laravel-将列添加到现有透视表并添加MUL键
我有一个Laravel pivot表,其中包含多个主键:“order\u id”、“product\u id”、“variation\u id” 我需要在此表中添加一个名为“id”的新列,具体操作如下:Php Laravel-将列添加到现有透视表并添加MUL键,php,sql,laravel,Php,Sql,Laravel,我有一个Laravel pivot表,其中包含多个主键:“order\u id”、“product\u id”、“variation\u id” 我需要在此表中添加一个名为“id”的新列,具体操作如下: public function up() { Schema::table('order_product', function (Blueprint $table) { $table->integer('id')->un
public function up()
{
Schema::table('order_product', function (Blueprint $table)
{
$table->integer('id')->unsigned()->after('variation_sku');
});
}
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']);
});
}
我的原始迁移文件如下所示:
public function up()
{
Schema::table('order_product', function (Blueprint $table)
{
$table->integer('id')->unsigned()->after('variation_sku');
});
}
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']);
});
}
这次迁移包括我的3个组合键
现在我需要添加新列“id”,并将该列添加到复合键中
我在Laravel文档中找不到任何内容,我怀疑这可能不可能,因为这可能会影响此表中的过去记录 您可以删除主键,然后创建一个新的主键:
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']);
});
}
您可以删除主键,然后创建一个新的主键:
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']);
});
}