【问题标题】:Laravel relationship - not unique table aliasLaravel 关系 - 不是唯一的表别名
【发布时间】:2018-09-28 09:03:34
【问题描述】:

我收到以下错误

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'contacts' (SQL: select * from (select `item_meta`.*, max(case when `item_meta`.`id` = test then 150 else 0 end + case when `item_meta`.`id` like test% then 50 else 0 end + case when `item_meta`.`id` like %test% then 10 else 0 end + case when `item_meta`.`name` = test then 120 else 0 end + case when `item_meta`.`name` like test% then 40 else 0 end + case when `item_meta`.`name` like %test% then 8 else 0 end + case when `contacts`.`name` = test then 45 else 0 end + case when `contacts`.`name` like test% then 15 else 0 end + case when `contacts`.`name` like %test% then 3 else 0 end + case when `contacts`.`name` = test then 45 else 0 end + case when `contacts`.`name` like test% then 15 else 0 end + case when `contacts`.`name` like %test% then 3 else 0 end) as relevance from `item_meta` left join `contact_manufacturer_to_meta` on `contact_manufacturer_to_meta`.`meta_id` = `item_meta`.`id` left join `contacts` on `contact_manufacturer_to_meta`.`contact_id` = `contacts`.`id` left join `contact_vendor_to_meta` on `contact_vendor_to_meta`.`meta_id` = `item_meta`.`id` left join `contacts` on `contact_vendor_to_meta`.`contact_id` = `contacts`.`id` where (`item_meta`.`id` like %test% or `item_meta`.`name` like %test% or `contacts`.`name` like %test% or `contacts`.`name` like %test%) and `item_meta`.`company_id` = 1 group by `item_meta`.`id`) as `item_meta` where `relevance` >= 6.00 and `item_meta`.`deleted_at` is null order by `relevance` desc limit 10)

这是因为我有两个数据透视表的同一张表,数据库结构如下。

Schema::create('contacts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('contact_manufacturer_to_item', function (Blueprint $table) {
        $table->integer('item_id')->unsigned();
        $table->integer('contact_id')->unsigned();
        $table->string('manufacturer_part_number')->nullable();
        $table->timestamps();

        $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');

        $table->primary(['item_id', 'contact_id']);
    });

Schema::create('contact_vendor_to_item', function (Blueprint $table) {
        $table->integer('item_id')->unsigned();
        $table->integer('contact_id')->unsigned();
        $table->string('vendor_part_number')->nullable();
        $table->timestamps();

        $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');

        $table->primary(['item_id', 'contact_id']);
    });

导致问题的控制器代码基于用于搜索的 Eloquence 包

return Item::with('vendors')->with('manufacturers')->search($query, [
                'id' => 10,
                'name' => 8,
                'manufacturers.name' => 3,
                'vendors.name' => 3,
            ])
            ->limit($limit)
            ->get();

型号代码

protected $table = 'item_meta';

public function vendors() {
    return $this->belongsToMany('App\Contact', 'contact_vendor_to_meta', 'meta_id', 'contact_id');
}

public function manufacturers() {
    return $this->belongsToMany('App\Contact', 'contact_manufacturer_to_meta', 'meta_id', 'contact_id');
}

我可以做些什么来解决这个问题,而不必为搜索执行 RAW SQL 查询?

【问题讨论】:

  • 请贴出整个错误信息(包括查询SQL)。
  • @JonasStaudenmeir 我将在明天添加整个查询。
  • @JonasStaudenmeir 我更新了错误以包含查询。
  • 请贴出Item模特的代码。
  • @JonasStaudenmeir 添加了 Item 模型的相关代码。

标签: php sql laravel eloquent


【解决方案1】:

这是你雄辩的查询的直译:

return Item::with('vendors' => function ($query) {
  $query->where('name', 3);
}])
  ->with('manufacturers' => function ($query) {
    $query->where('name', 3);
})
  ->where([['id' => 10], ['name', 8]])
  ->limit($limit)
  ->get();

但这也行不通。您可能想要实现的是:

return Item::with('vendors' => function ($query) {
  $query->where('id', 3); //$vendor_id or $vendor_name?
}])
  ->with('manufacturers' => function ($query) {
    $query->where('id', 3); //$manufacturer_id or $manufacturer_name?
})
  ->where('id', 10) //$item_id or $item_name?
  ->where('name', 8)
  ->limit($limit)
  ->get();

目前还不清楚您要搜索什么。如果您想要 id(或名称)或项目,则无需在供应商或制造商中搜索。您的 name 令人困惑,因为它是一个整数,而不是一个字符串。您的迁移架构中没有名称。 manufacturersvenders 中有这些名称吗?这很令人困惑。

【讨论】:

  • 制造商和供应商与联系人表是多对多关系。并且发生错误是因为它试图将联系人设置为别名两次。他们的关系通过 eloquence 包得到解决。
猜你喜欢
  • 2017-09-17
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多