【发布时间】: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 模型的相关代码。