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

Stale cached value returned where(...)->first() #453

Open
aaroncameron opened this issue Aug 2, 2023 · 6 comments
Open

Stale cached value returned where(...)->first() #453

aaroncameron opened this issue Aug 2, 2023 · 6 comments
Assignees

Comments

@aaroncameron
Copy link

Describe the bug
Requesting the first (and unique) result with Model::where(...)->first() returns an old, incorrect result,

Eloquent Query

MyModel::where(['a'=>1,'b'=>2])->first();  // returns an old result

MyModel::where(['a'=>1,'b'=>2])->get()->first(); // returns the correct result.

Environment

  • PHP: 8.1.2
  • OS: Ubuntu 22.04
  • Laravel: 10.15.0
  • Model Caching: 0.13.4

Additional context
That the result in my test case is unique may or may not be relevant. Have not constructed a test where it is not to confirm.

There is in the above a unique key across a,b. The result is not simply coincidentally unique, but is by definition.

@aaroncameron
Copy link
Author

Just to update with another case discovered, for non-unique result sets, different symptom and way to trigger:

MyModel::where('cond', true)->first(); // returns no results
MyModel::where('cond', true)->get()->first(); // returns the correct result
MyModel::where('cond', true)->limit(1)->get()->first(); // also returns no results.

@mikebronner
Copy link
Owner

thanks for the added details Aaron. These should not be happening. I will take a look at the tests later today and see what's going on.

@mikebronner
Copy link
Owner

I've been doing some testing and have found the following:

  • queries using the ->where([ <multiple conditions> ]) syntax are not caching correctly, when compared to ->where(...)->where(...)
  •         $author = (new Author)
              ->where("id", 1)
              ->orWhere("id", 2)
              ->first();
          $author2 = (new Author)
              ->where("id", 2)
              ->orWhere("id", 1)
              ->get()
              ->first();
    
    seems to return the same results as expected.

Could you send me the full query you are having issues with? Perhaps that will give me some additional clues to look for.

@aaroncameron
Copy link
Author

Sure thing. I don't recall the code that was the source of my original case any longer, but the example I sent here was contrived anyway. Confirmed, but contrived. The version of the issue I found earlier today I still have though. TL;DR is it looks like the limit causes the cache to fail in some way.

Looks to me like the conditions don't even matter (other than the issue you found).

In all cases I confirmed the working/not working with the cache turned on, then turned the cache off with the query logging on to get the exact query generated.

Working:

DND5ECharacterSheet::where('is_premade', true)->get()->first();
select * from `dnd5e_character_sheets` where `is_premade` = ?
-- {"bindings":[true],"time":14.59} 

Not working:

DND5ECharacterSheet::where('is_premade', true)->limit(1)->get()->first();
select * from `dnd5e_character_sheets` where `is_premade` = ? limit 1
-- {"bindings":[true],"time":9.35} 

Not working:

DND5ECharacterSheet::where('is_premade', true)->first();
select * from `dnd5e_character_sheets` where `is_premade` = ? limit 1
-- {"bindings":[true],"time":6.52} 

@ricdizio
Copy link

is there any updates about this issue? I still seeing this problem with latest version of laravel

@mikebronner
Copy link
Owner

Sorry, I haven't had time to look into this yet. If someone would like to take a stab at a PR that would help to move things forward more quickly.

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

No branches or pull requests

3 participants