Cast on Updated At column not applied for rapid saves (i.e. unit tests) #47506
Unanswered
willpower232
asked this question in
General
Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We are making use of a legacy database written for an ancient framework in Laravel. One "feature" of this framework is that integer unix timestamps are used for the updated_at column instead of datetime strings.
We can deal with this by setting
protected $dateFormat = 'U';
on the model classes however ideally we would like to use datetimes for application-specific time-related columns going forward so don't want to force the format on all new columns.We wrote a cast to juggle the value from an instance of Carbon to
$carbon->timestamp
and all seemed well until we went to write unit tests for the functionality.The tests failed because data was being truncated for the updated_at column as it was receiving a datetime string instead of the timestamp from the cast.
I believe this is occurring here as it does not seem to check for casts before appending an updated_at value
framework/src/Illuminate/Database/Eloquent/Builder.php
Line 1140 in 31b3d29
I think that updated_at is not marked as dirty because the value has not changed because the change has been within the same second as the creation or another update.
A quick resolution to this problem within tests is to include
$this->travelTo(now()->addMinute());
before every change made to a particular model so that the casted value is picked up with the dirty attributesframework/src/Illuminate/Database/Eloquent/Model.php
Line 1212 in 31b3d29
Obviously this is not possible within the main application so this problem may occur there if there are saves within the same second from different parts of the code.
I am unsure as to whether the resolution is for the builder to cast the value before appending it or for any dirty attributes check to always include the updated_at value if any other changed values are present.
Should I make a bug issue for this?
Beta Was this translation helpful? Give feedback.
All reactions