Skip to content
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

Model date cast causes updated_at to refresh on datetime (same date) value update #49720

Closed
ed-hanton-swytch opened this issue Jan 17, 2024 · 3 comments

Comments

@ed-hanton-swytch
Copy link

ed-hanton-swytch commented Jan 17, 2024

Laravel Version

10.35.0

PHP Version

8.2.8

Database Driver & Version

MySQL 8.0.30 Ubuntu via Sail

Description

Saving a value which is cast to a date twice on the same day causes the updated_at value to be update to the second time. I would expect the updated_at to remain at the first instance as no value has changed in the model.

I'm not sure if this is a problem related to casting, parameter setting logic, saving isDirty logic, or database connection.
I believe the root of the problem is that the value is not correctly cast into a date, and instead is always treated as a datetime.

Looking at

return $this->fromDateTime($attribute) ===

/**
     * Determine if the new and old values for a given key are equivalent.
     *
     * @param  string  $key
     * @return bool
     */
    public function originalIsEquivalent($key)
    {
        if (! array_key_exists($key, $this->original)) {
            return false;
        }

        $attribute = Arr::get($this->attributes, $key);
        $original = Arr::get($this->original, $key);

        if ($attribute === $original) {
            return true;
        } elseif (is_null($attribute)) {
            return false;
        } elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) {
            return $this->fromDateTime($attribute) ===
                $this->fromDateTime($original);
 

All date casts are treated the same for isDirty purposes which may be pushing it into the array for updates despite being the same value?

Equally I'm not sure if this is all by design because I think when using a backing sqlite database which doesn't have a date type, it is still stored as the original datetime.. Which seems counterintuitive to how casts should act as otherwise what is the difference between a date and datetime cast?

Steps To Reproduce

Create a model with a property of date_column with a date cast

$casts = [
    'date_column' => 'date'
]

If we save the model with a new date_column value using $model->update(['date_column' => now()]), the model will save and change the updated_at to now as expected.
If then after a few seconds we save the model again $model->update(['date_column' => now()]), the model will again be saved, and the updated_at changed, but the column value has not changed.

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@devcircus
Copy link
Contributor

Is the db actually storing only the date? The now() function gives date and time. The cast just transforms the data when it is accessed.

> now();
= Illuminate\Support\Carbon @1705968914 {#6714
    date: 2024-01-23 00:15:14.516627 UTC (+00:00),
  }

If the database is actually storing the full datetime, then it makes sense that the updated_at column would change.

@crynobone
Copy link
Member

Hey there,

We're closing this issue because it's inactive, already solved, old, or not relevant anymore. Feel free to open up a new issue if you're still experiencing this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants