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

updated_at field is filled with the current timestamp when inserting new data along with created_at #4038

Closed
fahmiegerton opened this issue Dec 29, 2020 · 4 comments

Comments

@fahmiegerton
Copy link

Please ask support questions on our forum.
We use github issues to track bugs and planned work.

Well I can't log in on there. Here's the screenshot:
image

So, here it is. I'm just saying it straight away. The updated_at field is filled with the current timestamp when inserting new data, while it shouldn't. I don't know what I'm misconfigured of but, been tried everything, seems same. Here's the capture:
image

And here's the model:
image

And here's the data type:
image

I hope someone could help me. I need a date of when the data is updated, but seems for now I need to do it manually. Thank you.

@paulbalandan
Copy link
Member

Based on the Model's code, during data inserts the created_at and updated_at fields are both set with the same time during creation. The updated_at field will only get updated of there is an actual update of data.

@fahmiegerton
Copy link
Author

fahmiegerton commented Dec 29, 2020

Based on the Model's code, during data inserts the created_at and updated_at fields are both set with the same time during creation. The updated_at field will only get updated of there is an actual update of data.

Shouldn't be NULL like the deleted_at ? Why would it be set on both field?

@paulbalandan
Copy link
Member

I'm not the proper person to answer the logic behind the setting of updated_at but I'm guessing it is based on the assumption that update is technically the same with creation in terms of starting point. I mean is if you update something which isn't there in the first place is the same with creating it. Your point is valid but I'm afraid the current behavior cannot be changed now as it may be breaking for users who are already relying in this behavior.

If you want to circumvent this behavior without adding additional code, you can do is add the updated_at field to the data you're inserting. The default behavior is the updated_at field is set automatically during inserts when it is not found in the $data array.

You can do something like this:

$data = [
    'name' => 'foo',
    'description' => 'bar',
    'updated_at' => null, // don't forget to add this line to force not setting `updated_at` with same date as `created_at`
];

// insert the data using our model
$model->insert($data);

@lonnieezell
Copy link
Member

You didn't mis-configure. That's by design. That allows you to include newly created records when sorting by the most recently updated.

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

No branches or pull requests

3 participants