gpt4 book ai didi

mysql - 更新 end_time 时,字段 start_time 会自动更新

转载 作者:行者123 更新时间:2023-11-29 09:27:48 24 4
gpt4 key购买 nike

我的项目使用的是 laravel 6 和 mysql 5.7,我在模型中放置了两列 start_timeend_time ,并且都在 timestamp 类型。在操作中,我创建一条记录并初始化 start_time 值。在另一个操作中,我更新了该记录的 end_time 列。
这是我创建项目时的代码:

DeviceUsage::create([
'user_id' => $user->id,
'device_id' => $request->input('device_id'),
'start_time' => date('Y-m-d H:i:s'),
]);

这是查询的日志:

'query' => 'insert into `device_usages` (`user_id`, `device_id`, `start_time`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?)',
'bindings' =>
array (
0 => 3,
1 => '1',
2 => '2019-12-11 10:33:51',
3 => '2019-12-11 10:33:51',
4 => '2019-12-11 10:33:51',
),
'time' => 0.68,

创建命令后的数据库:

+----+-----------+---------------------+----------+
| id | device_id | start_time | end_time |
+----+-----------+---------------------+----------+
| 1 | 2 | 2019-12-11 11:26:53 | NULL |
+----+-----------+---------------------+----------+

这是我的更新代码:

$deviceUsage = DeviceUsage::where('device_id', $oldDeviceId)->where('user_id', $user->id)->whereNull('end_time')->first();
if (!empty($deviceUsage)) {
$deviceUsage->update(['end_time' => date('Y-m-d H:i:s')]);
}

并更新查询日志:

'query' => 'update `device_usages` set `end_time` = ?, `device_usages`.`updated_at` = ? where `id` = ?',
'bindings' =>
array (
0 => '2019-12-11 10:33:51',
1 => '2019-12-11 10:33:51',
2 => 12,
),
'time' => 1.41,

更新命令后的数据库:

+----+-----------+---------------------+---------------------+
| id | device_id | start_time | end_time |
+----+-----------+---------------------+---------------------+
| 1 | 2 | 2019-12-11 14:59:24 | 2019-12-11 11:29:24 |
+----+-----------+---------------------+---------------------+

我不知道为什么 start_time 会更新为 end_time 的本地时间(我的本地时间是 UTC+3:30)。

最佳答案

您检查过该字段的默认值吗?是否设置为“更新时的当前时间戳”

关于mysql - 更新 end_time 时,字段 start_time 会自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com