-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Cast on Updated At column not applied for rapid saves (i.e. unit tests) #47769
Comments
Thanks. The |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
@driesvints thanks for confirming, would you be able to nominate what you think the correct solution is?
I feel like the latter probably makes more sense, if there are dirty attributes present then include the updated_at which already seems to have been through the casts, but happy to look at the other option if that makes more sense to you. |
I'm not sure myself, sorry. I'd say the former because the latter could have side effects that people don't want. |
what do you think about this? |
@willpower232 |
@amir9480 unfortunately its definitely eloquent related |
We can follow along with this fix I have just created: #47942 |
Laravel Version
10.13.2
PHP Version
8.2.7
Database Driver & Version
No response
Description
apologies for duplicating an unanswered discussion #47506 but I believe this is a bug requiring attention
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.
if you create an entity in the database and then update the entity via a queued job then you encounter this error and must delay the job being dispatched
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.
Steps To Reproduce
Have the following database schema and code
The text was updated successfully, but these errors were encountered: