-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.2] Adds idType to model in order to prevent assumptions about ids #13985
Conversation
@@ -71,6 +71,13 @@ | |||
public $incrementing = true; | |||
|
|||
/** | |||
* Sets the type used by the ID. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make it clear that this property is ignored when incrementing is not enabled.
@@ -71,7 +71,8 @@ | |||
public $incrementing = true; | |||
|
|||
/** | |||
* Sets the type used by the ID. | |||
* Sets the type used by the ID. Note that this property is ignored unless |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please start a new line here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, just make the description this: The type used by the incrementing ID.
. It's pretty clear then.
Any idea why the build is failing here? It's throwing a bunch of these:
but I'm not entirely sure how this patch would be affecting anything to do with connections... |
@@ -2763,7 +2770,7 @@ public function getCasts() | |||
{ | |||
if ($this->getIncrementing()) { | |||
return array_merge([ | |||
$this->getKeyName() => 'int', | |||
$this->getKeyName() => $this->getIdType(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->$idType
;)
Yeh, I've left a comment inline. |
Yeh, the error is a bit cryptic due to our |
doh! it's probably something to do with that bad getter call. |
Yeh, that was exactly it. ;) |
Looks like it should work now. Could you add a couple of tests please, then it'll be a 👍 from me. |
I wasn't entirely sure how to unit test this at first but I think I figured something out. Let me know what you think of the two added in the latest commit (or just merge if they seem 👍 ). |
@stevenleeg If your model doesn't have an auto-incrementing integer key you should set: public $incrementing = false; I don't see any good use case for |
@acasar setting Currently this also causes ids to be typecasted to |
It can be auto-incrementing, but not an integer. |
Ah yes, you're right :) |
@acasar glad we could clear things up :) @GrahamCampbell is there anything else I can do/clear up to get this merged? also mega thanks for the quick feedback! |
@stevenleeg It looks good to me. It's up to Taylor to merge this though, if he likes it. ;) |
@stevenleeg Thanks for the contribution. ❤️ |
I could be wrong... but I think only the tests for this fix got merged into 5.2 Merge Commit -81a8482 (The tests pass because of the stub...maybe worth adding failure case tests?) |
oops nvm I'm wrong, I just noticed |
Hi there,
We recently upgraded to Laravel 5.2 and noticed some weird behavior when trying to save models and tracked it down to an incorrect assumption made in a recent change to
\Illuminate\Database\Model
, namely in thegetCasts()
function:Our codebase uses string uuids generated at the database layer (postgres) rather than the application layer, so technically we needed
$incrementing
to be set to true (since the database was handling "incrementing" in our case), however our ID type wasstring
rather thanint
.As an interim solution we just added the following to a
BaseModel
class we created which all of our models inherit from:this just overrides Laravel's getCasts function to get rid of the
int
typecast on the primary key. It worked but felt kind of jank, and we're sure we're not the only ones who have this use case. This PR proposes a solution which keeps Laravel's defaults, but adds the option to set$idType
on models and override the default type (int
) which is currently assumed to be true.This shouldn't break anything for anyone else, it just adds this extra option which helps people with non-
int
ID types that are generated by the database rather than the application.Let me know if you have any thoughts, questions, etc. I'd love to get this merged in (or at least start a discussion on how to best handle the issue). Thanks!