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

[Known Issue] Duplicate conditions on retrying SELECT calls under createOrFirst() #48718

Closed
mpyw opened this issue Oct 12, 2023 · 7 comments · Fixed by #48725
Closed

[Known Issue] Duplicate conditions on retrying SELECT calls under createOrFirst() #48718

mpyw opened this issue Oct 12, 2023 · 7 comments · Fixed by #48725

Comments

@mpyw
Copy link
Contributor

mpyw commented Oct 12, 2023

Laravel Version

10.x (>10.28.0 <10.29.0: 4b4b1e4)

PHP Version

irrelevant

Database Driver & Version

irrelevant

Description

The known issue in:

Notes

Some FIXME comments are remaining.

$model->getConnection()
    ->expects('select')
    // FIXME: duplicate conditions
    ->with('select * from "table" where ("attr" = ?) and ("attr" = ?) limit 1', ['foo', 'foo'], false)
    ->andReturn([[
        'id' => 123,
        'attr' => 'foo',
        'val' => 'bar',
        'created_at' => '2023-01-01 00:00:00',
        'updated_at' => '2023-01-01 00:00:00',
    ]]);

These indicate a problem with duplicate and useless conditions being given when retrying the SELECT, but do not significantly bother with the operation. Attempting to fix this using clone may yield some unexpected effects due to the Builder mutableness. Therefore, we'd like to recognize this as a known issue and leave it out of the scope of this PR.

@tonysm If you think you can fix this problem, could you please submit another PR? Thank you in advance.

In #48637, although we knew it was a known issue, did not fix it to minimize risk. We will probably clone Builder to fix it, but we need to carefully examine the scope of the impact.

There is no real harm in the operation, since it only adds useless conditions to the query. Therefore, this is more of a refactoring than a bug fix.

@mpyw mpyw changed the title [Known Issue] Duplicate conditions on createOrFirst [Known Issue] Duplicate conditions on createOrFirst() calls Oct 12, 2023
@mpyw mpyw changed the title [Known Issue] Duplicate conditions on createOrFirst() calls [Known Issue] Duplicate conditions on retrying SELECT calls under createOrFirst() Oct 12, 2023
@mpyw
Copy link
Contributor Author

mpyw commented Oct 12, 2023

If the impact is greater than expected, consider targeting for 11.x.

@driesvints
Copy link
Member

I don't understand. Is there a bug here at all? Did you now create a bug issue for a PR you sent in yourself? Can you send in a PR with your suggested fix?

@mpyw
Copy link
Contributor Author

mpyw commented Oct 13, 2023

@driesvints Sorry for the confusion. Actually:

@mpyw
Copy link
Contributor Author

mpyw commented Oct 13, 2023

@tonysm If you have time, could you please make the fix? Thank you in advance.

@tonysm
Copy link
Contributor

tonysm commented Oct 13, 2023

My personal life is quite demanding right now, but I'll try to find an hour or two to dig into this.

The issue doesn't seem to be on the createOrFirst, right? It sounds like when the createOrFirst is used inside the firstOrCreate, it carries the original where attributes from the first find attempt, which causes the duplication (firstOrCreate adds a ->where() which gets carried to the last find query used in createOrFirst when it attempts to find the record, duplicating the where clause). It's not a bug, because the query still works. But something we should clean up.

I think cloning the query builder (as suggested in the PR as a possible fix) on the first attempt should resolve the issue, but we need more testing.

@mpyw
Copy link
Contributor Author

mpyw commented Oct 13, 2023

The issue doesn't seem to be on the createOrFirst, right? It sounds like when the createOrFirst is used inside the firstOrCreate, it carries the original where attributes from the first find attempt, which causes the duplication (firstOrCreate adds a ->where() which gets carried to the last find query used in createOrFirst when it attempts to find the record, duplicating the where clause).

Sorry, that's right. Actually #47973 contained the firstOrCreate improvement but already reverted, and most recently reappeared in #48637.

Greatly appreciate for you help.

@github-actions
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

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