【问题标题】:Laravel relationship belongsToMany with composite primary keysLaravel 关系 belongsToMany 与复合主键
【发布时间】:2018-01-31 03:00:09
【问题描述】:

我有 3 个表,我正在尝试在 order_products 和 order_products_status_names 之间建立关系。我有名为 order_product_statuses 的转换/数据透视表。问题是我的 PK,因为我有表订单 3 Pk,我不知道如何通过关系连接这 3 个表。 我的迁移是:

餐桌订购产品:

 public function up()
{
    Schema::create('order_products', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->integer('size');

        $table->primary(['order_id', 'product_id', 'ordinal']);
        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
    });
}

表格订单产品状态 - 这是我在 order_products 和 order_product_status_names 之间的转换/数据透视表

 public function up()
{
    Schema::create('order_product_statuses', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('status_id')->references('id')->on('order_product_status_names');
        $table->primary(['order_id', 'product_id', 'ordinal']);
    });
}

最后一个是订单产品状态名称

public function up()
{
    Schema::create('order_product_status_names', function (Blueprint $table) {
        $table->integer('id')->unsigned();
        $table->string('name');
        $table->string('code');
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
    });
}

我知道这里有两种关系 blengsToMany,但我不知道或者我可以声明这种关系(从 order_products 到 order_product_status_names 和 inverse )?

【问题讨论】:

  • 明天早上会仔细看看你的需求,看看能不能帮到你。
  • 只是一个简单的问题,为什么您在 order_product 和 order_product_statuses 中都有 order_id 和 product_id,这是出于性能原因吗?
  • 谢谢!我会等待并感谢它!
  • Eloquent 确实不是为复合键设计的。通过使您的数据库结构符合每个表中的 id 主键,您将省去很多麻烦。
  • 是的,因为我有 3 个 PK(order_id、product_id 和普通),当客户订购 2 个相同尺寸的相同产品时,我有 2 个不同的行,只有序数可以将它们与 1 区分开命令。当客户只从他们那里拿走一个时,我可以添加单独的状态

标签: php laravel relationship composite-primary-key


【解决方案1】:

好吧,我并没有在这上面花太多时间,但这就是我会做的。同样正如@Devon 提到的,我可能会为每个表添加 id,因为 Eloquent 并不是真正为复合键设计的。正如我的一个 cmets 中提到的,我通常会创建启动和更新脚本,因此语法可能并不完全正确:

public function up() {
    Schema::create('order_products', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('order_product_statuses_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->integer('size');

        $table->primary('id');
        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
        $table->foreign('order_product_statuses_id')->references('id')->on('order_product_statuses');
    });
}

public function up() {
    Schema::create('order_product_statuses', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
        $table->foreign('status_id')->references('id')->on('order_product_status_names');
    });
}

public function up() {
    Schema::create('order_product_status_names', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->string('name');
        $table->string('code');
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
    });
}

HTH 你出去一点。

【讨论】:

  • 附带说明,如果您想要一门关于创建数据库的良好入门课程,您应该使用 Google MongoDB 大学并注册免费的 M101N 课程。我从事数据库开发已经有一段时间了,这对我来说仍然是一门很棒的课程,而且我实际上学到了很多东西。
  • 你是对的,我不得不改变我的桌子......感谢帮助
  • 我遇到了同样的问题,我不想将自动增量 id 作为主键,因为这真的限制了箭头的数量?正确的 ?当我们遇到这种问题并且不想使用自动增量时,我们该怎么办?
  • @Toinou 我不太明白你在评论中指的是什么“......这真的限制了箭头的数量?”?我通常会尝试找出集合/表/等中的独特之处并将其用作主键。我认为使用自动增量作为主键的主要原因是因为它使分页更容易并且是独一无二的,尽管这只是我的想法&我可能完全偏离轨道;-)。如果这不能回答您的问题,请提出一个新问题并在此处提供该问题的链接,以便社区/我可以尝试帮助您。希望对您有所帮助(HTH)。
猜你喜欢
  • 2015-01-16
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 2020-01-08
相关资源
最近更新 更多