Skip to content

Conversation

@mattstauffer
Copy link
Contributor

If a model is incrementing, set a default to cast the primary key to type integer.

This fixes an inconsistency where an instance created with Model::create would have an integer ID, but model instances fetched from the database would have a string ID.

Written while pairing with @adamwathan.

Also fixes another test which broke when implementing this--it was setting the primary key of an incrementing table to the value 'foo', which is invalid. Updated the test to use a non-incrementing model.

@GrahamCampbell GrahamCampbell changed the title Cast incrementing IDs to Integer by default [5.2] Cast incrementing IDs to Integer by default Dec 10, 2015
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we used the more conventional int here.

@mattstauffer
Copy link
Contributor Author

@GrahamCampbell Fixed--thanks!

@JosephSilber
Copy link
Contributor

Do we really need this in every call to getCasts?

Why not do it once in the constructor?

@adamwathan
Copy link
Contributor

@JosephSilber getDates works the same way, so I think either both should be done at construction or on the fly.

@JosephSilber
Copy link
Contributor

@adamwathan now that we can do dates in the casts, we should be doing all of that in the constructor.

@GrahamCampbell
Copy link
Collaborator

getDates works the same way, so I think either both should be done at construction or on the fly.

Maybe we should change them both in 5.2 in the interest of efficiency?

@GrahamCampbell
Copy link
Collaborator

Maybe leave that to another PR though, to be discussed after this is either rejected or accepted.

@taylorotwell
Copy link
Member

I agree we'll probably need to do this one time. How do we want to handle that?

@taylorotwell taylorotwell merged commit 1448fd3 into laravel:5.2 Dec 14, 2015
@sherbrow
Copy link

This was unexpected.
Before: incrementing only meant that the insert would trigger a get to set the newly generated value in the model, and in that case where it was not a integer, the value would not change
After: incrementing means integer casting

It breaks previously non-integer primary keys when incrementing was let to the default true value.
Not the biggest change, but still worth mentioning (and incrementing seems not to be documented before that breaking change in 5.2)

Notes: Model (5.1) and Model@insertAndSetId (5.1)

Edit:
There was only a conditional cast before, in processor there
Shouldn't that test (is_numeric) be reproduced somehow instead of forcing the cast ?

@adamwathan
Copy link
Contributor

@sherbrow Are there any cases where would you want $incrementing = true with a non-integer key? Or is it more a matter of not doing the cast if someone is using a non-integer key and forget to disable $incrementing?

@sherbrow
Copy link

@adamwathan It's definitely the case where one would forget to disable $incrementing, as stated, I don't think it was in the docs before 5.2.
It appears in the upgrade guide, so it's probably my fault not to take that into account in the first place.

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

Successfully merging this pull request may close these issues.

6 participants