I'm generating a query from a selection in Nova which is used with Laravel-Nova-Excel in an action to generate a CSV export. I'm not sure whether this problem is coming from Nova itself or that package, but Nova has had this bug in the past which is almost identical.
My query method, which is based on a Scan resource:
public function query()
{
return $this->query
->join('locations', 'locations.id', '=', 'scans.location_id')
->leftJoin('customers', 'locations.customer_id', '=', 'customers.id')
->join('users', 'users.id', '=', 'scans.user_id')
->leftJoin('product_scan', 'scans.id', '=', 'product_scan.scan_id')
->leftJoin('products', 'products.id', '=', 'product_scan.product_id')
->leftJoin('skus', 'products.sku_id', '=', 'skus.id')
->leftJoin('borrowings', 'borrowings.scan_id', '=', 'scans.id')
->select(
[
'scans.uuid',
'scans.status',
'scans.created_at',
'scans.external_id',
'users.email as user_email',
'customers.name as customer_name',
'locations.name as location_name',
'locations.uuid as location_uuid',
'skus.brand as sku_brand',
'skus.name as sku_name',
'skus.type as sku_type',
'products.serial as product_serial',
'borrowings.status as borrowing_status',
'borrowings.sku_type as borrowing_sku_type',
]
);
}
This generates an error:
Syntax error or access violation: 1066 Not unique table/alias: 'locations' (
Connection: mysql, SQL: select `scans`.`uuid`, `scans`.`status`, `scans`.`created_at`, `scans`.`external_id`, `users`.`email` as
`user_email`, `customers`.`name` as `customer_name`, `locations`.`name` as `location_name`, `locations`.`uuid` as `location_uui
d`, `skus`.`brand` as `sku_brand`, `skus`.`name` as `sku_name`, `skus`.`type` as `sku_type`, `products`.`serial` as `product_ser
ial`, `borrowings`.`status` as `borrowing_status`, `borrowings`.`sku_type` as `borrowing_sku_type` from `scans` inner join `loca
tions` on `locations`.`id` = `scans`.`location_id` left join `customers` on `locations`.`customer_id` = `customers`.`id` inner j
oin `users` on `users`.`id` = `scans`.`user_id` left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id` left join `p
roducts` on `products`.`id` = `product_scan`.`product_id` left join `skus` on `products`.`sku_id` = `skus`.`id` left join `borro
wings` on `borrowings`.`scan_id` = `scans`.`id` inner join `locations` on `locations`.`id` = `scans`.`location_id` left join `cu
stomers` on `locations`.`customer_id` = `customers`.`id` inner join `users` on `users`.`id` = `scans`.`user_id` left join `produ
ct_scan` on `scans`.`id` = `product_scan`.`scan_id` left join `products` on `products`.`id` = `product_scan`.`product_id` left j
oin `skus` on `products`.`sku_id` = `skus`.`id` left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id` where `scans`.`i
d` in...
Cleaned up, the full set of joins is:
from `scans`
inner join `locations` on `locations`.`id` = `scans`.`location_id`
left join `customers` on `locations`.`customer_id` = `customers`.`id`
inner join `users` on `users`.`id` = `scans`.`user_id`
left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id`
left join `products` on `products`.`id` = `product_scan`.`product_id`
left join `skus` on `products`.`sku_id` = `skus`.`id`
left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id`
inner join `locations` on `locations`.`id` = `scans`.`location_id`
left join `customers` on `locations`.`customer_id` = `customers`.`id`
inner join `users` on `users`.`id` = `scans`.`user_id`
left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id`
left join `products` on `products`.`id` = `product_scan`.`product_id`
left join `skus` on `products`.`sku_id` = `skus`.`id`
left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id`
So while you can see there are no duplicate tables in my query builder code, there are duplicate tables in the generated query. If I replace any table name with an alias, for example:
->join('locations as locations2', 'locations2.id', '=', 'scans.location_id')
I get the same error on the new name.
This looks like a bug to me – I don't know how I would provoke this behaviour intentionally. What do you think?