准备的数据表

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;

准备的数据

idvideo_idsearch_countcreated_atupdated_at
1214748364702024-10-31 09:54:352024-10-31 09:54:35
2214748364702024-10-31 09:54:362024-10-31 09:54:36
3214748364702024-10-31 09:54:372024-10-31 09:54:37
4214748364702024-10-31 09:54:392024-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)

图片.png

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 更新


universe_king
3.4k 声望680 粉丝