准备的数据表
CREATE TABLE `found_video_cover_image_ocr_count` (
`id` bigint NOT NULL AUTO_INCREMENT,
`video_id` bigint NOT NULL,
`search_count` int NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `foundedvideocoverimageocrcounttable_video_id` (`video_id`)
) ENGINE=InnoDB AUTO_INCREMENT=151945318 DEFAULT CHARSET=utf8mb3;
准备的数据
| id | video_id | search_count | created_at | updated_at |
|---|---|---|---|---|
| 1 | 2147483647 | 0 | 2024-10-31 09:54:35 | 2024-10-31 09:54:35 |
| 2 | 2147483647 | 0 | 2024-10-31 09:54:36 | 2024-10-31 09:54:36 |
| 3 | 2147483647 | 0 | 2024-10-31 09:54:37 | 2024-10-31 09:54:37 |
| 4 | 2147483647 | 0 | 2024-10-31 09:54:39 | 2024-10-31 09:54:39 |
看看执行下面的代码之后,updated_at 是否会变化
from loguru import logger
from core.mysql.models import FoundedVideoCoverImageOcrCountTable
video_id = 2147483647
fvcisc, created = FoundedVideoCoverImageOcrCountTable.get_or_create(
video_id=video_id)
fvcisc:FoundedVideoCoverImageOcrCountTable
fvcisc.search_count+=1
fvcisc.save()
logger.info(fvcisc.search_count)
UPDATE
`found_video_cover_image_ocr_count`
SET
`video_id` = 2147483647,
`search_count` = 1,
`created_at` = '2024-10-31 09:54:35',
`updated_at` = '2024-10-31 09:54:35'
WHERE
(`found_video_cover_image_ocr_count`.`id` = 1)
这个 save 并不是只 update 被修改的字段,而是全量字段都做了保存,
所以不会触发带有 CURRENT_TIMESTAMP 的 update_at 更新
IT极限技术分享汇