[7.x] Cast primary key to string when $keyType is string - #33930
Merged
Conversation
Contributor
|
I'm not sure if casting the attribute is the best solution. You specified the |
driesvints
pushed a commit
that referenced
this pull request
Aug 27, 2020
* Cast primary key to string when $keyType is string * fix test * fix remaining tests
Member
|
@stevethomas FYI: this broke passing This should also have been sent to 6.x LTS (see our contribution guide). I've ported this to 6.x with an additional fix for the bug which will also make its way to 7.x: #34031 |
taylorotwell
pushed a commit
that referenced
this pull request
Aug 27, 2020
* [7.x] Cast primary key to string when $keyType is string (#33930) * Cast primary key to string when $keyType is string * fix test * fix remaining tests * Fix bug with whereKey null Co-authored-by: Steve Thomas <steve@codinglabs.io>
Contributor
Author
|
Noted @driesvints, i'll be sure to PR to both branches in the future. |
Member
|
@stevethomas you don't need to PR to both. We merge 6.x into 7.x and 7.x into master regularly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Given a model with a string primary key:
and then running this on the model:
I expect a
nullresult, but in fact I get the first row in the table.Looking at the underlying query, sure enough it runs
select * from payment_providers where id = 0; MySQL seems to have a quirk where it will return all rows when comparing a string to a0integer.MySQL aside, I think Laravel should take care of casting the primary key according to
$keyTypeto prevent unexpected results.Solution
This PR checks the primary key type, and casts to a string when required in
whereKey()andwhereNotKey().Caveats
This query will return a collection of all rows in the table. I wasn't sure how to deal with an array of IDs, so I dealt with the singular lookup only.