Skip to content
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

lazyById doesn't check availability of id column in database response and silently ends up with endless loop #48401

Closed
decadence opened this issue Sep 14, 2023 · 2 comments

Comments

@decadence
Copy link
Contributor

decadence commented Sep 14, 2023

Laravel Version

10.22.0

PHP Version

8.1.9

Database Driver & Version

MySQL 5.7.39 (Windows 11)

Description

If we use this code (we don't select id column):

// select without id
$users = User::select("name")->lazyById(10);

foreach($users as $user) {
    // endless loop (generator) because lazyById will always return elements from the first page
}

So foreach will fall into endless loop and never ends if there are more than 1 page of results.

You can say that it's programmer's responsibility to include id in select but chunkById has this check and lazyById documentation doesn't say anything about id is required.

In the chunkById we have this code which verifies that response has id column.

$lastId = data_get($results->last(), $alias);

if ($lastId === null) {
    throw new RuntimeException("The chunkById operation was aborted because the [{$alias}] column is not present in the query result.");
}

It seems logical to me to add such a check to lazyById as well.

Steps To Reproduce

Create table with 100 records for example

// select without id
$users = User::select("name")->lazyById(10);

foreach($users as $user) {
    // endless loop (generator) because lazyById will always return elements from the first page
}
@decadence decadence changed the title lazyById doesn't check availability of id in database response lazyById doesn't check availability of id column in database response Sep 14, 2023
@decadence decadence changed the title lazyById doesn't check availability of id column in database response lazyById doesn't check availability of id column in database response and silently ends up with endless loop Sep 14, 2023
@crynobone
Copy link
Member

Seems like this is more a configuration issue rather that actual bug. The method name itself indicate the it would cursor by ID.

@decadence
Copy link
Contributor Author

decadence commented Sep 15, 2023

@crynobone What configuration do you mean? Did you read the issue? chunkById has this check, lazyById does not. This is inconsistent behaviour that leads to the endless loop.

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

No branches or pull requests

2 participants