Skip to content

[9.x] Unnecessary check for columns when paginating with joins #36561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

PauBenetPrat
Copy link

@PauBenetPrat PauBenetPrat commented Mar 11, 2021

This was added on this PR #32691 at this specific commit:
98a242e

A part from that, there is no way to select null columns, always
receiving array(null) instead.

On the other hand, if we join tables and use select *, we could be using
diferent fields from each table but it always breaks when paginating because of
duplicate column name 'id'.

…gination.

This was added on this PR laravel#32691 at this specific commit:
laravel@98a242e

A part from that, there is no way to select null columns, always
receiving array(null) instead.

On the other hand, if we join tables and use select *, we could be using
diferent fields from each table but it always breaks when paginating because of
duplicate column name 'id'.
@GrahamCampbell GrahamCampbell changed the title Remove unnecessary check if clone->columns exists as it's just for pa… [9.x] Remove unnecessary check if clone->columns exists as it's just for pa… Mar 11, 2021
@PauBenetPrat PauBenetPrat changed the title [9.x] Remove unnecessary check if clone->columns exists as it's just for pa… [9.x] Unnecessary check for columns when paginating with joins. Mar 11, 2021
@taylorotwell
Copy link
Member

Give me an example query that breaks currently.

@GrahamCampbell GrahamCampbell changed the title [9.x] Unnecessary check for columns when paginating with joins. [9.x] Unnecessary check for columns when paginating with joins. Mar 11, 2021
@GrahamCampbell GrahamCampbell changed the title [9.x] Unnecessary check for columns when paginating with joins. [9.x] Unnecessary check for columns when paginating with joins Mar 11, 2021
@PauBenetPrat
Copy link
Author

It's on the test @taylorotwell testBasicJoinsWithPaginateGroupingAndSelectingAsterisk.

With current code it returns:

select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" group by "user_id") as "aggregate_table"

And this returns Duplicate column id error.

Thanks!

@taylorotwell
Copy link
Member

Can you provide the real Eloquent query from your application that is breaking?

@PauBenetPrat
Copy link
Author

PauBenetPrat commented Mar 12, 2021

Hi, here is my query:

DB::table('orders')
  ->leftJoin('order_contents','order_contents.order_id', '=', 'orders.id')
  ->groupBy('orders.created_at')->select('*')->paginate(20)

Results into this:

Illuminate\Database\QueryException with message 'SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id' (SQL: select count(*) as aggregate from (select * from `GS_orders` left join `GS_order_contents` on `GS_order_contents`.`order_id` = `GS_orders`.`id` group by `GS_orders`.`created_at`) as `aggregate_table`)'

Would it help if I add my Order and OrderContent model definition?

@taylorotwell
Copy link
Member

To me it just looks like your query is broken? Select the columns you want and resolve the ambiguity?

@PauBenetPrat
Copy link
Author

Hi @taylorotwell,
It make no sense to me that paginating a query fails and not paginating it not:

$query =  DB::table('orders')
  ->leftJoin('order_contents','order_contents.order_id', '=', 'orders.id')
  ->groupBy('orders.created_at')->select('*');

$query->paginate(20);  // FAILS

$query->get();       // SUCCESS

Thats just an example of a query that fails. We've got this kind of queries on a 100 reports (more or less) and them are selecting *. After the select we use the fields that we need and when there is an ambiguity error we specify which table should be used.

For example we've got ContentReport and OrderReport which extends one from the other. Now a days we are using a select * on OrderReport.

To select the appropriate tables now we should select orders.* on OrderReport and order_contents.* (overriding parent method) on ContentReport. That may seem simple, but we can not assure all results will stay the same on production 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants