CREATE TABLE `found_video_cover_image_ocr_count` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `video_id` int 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`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

测试数据如下:

video_id 没有添加唯一约束,且存在多个一样的值

图片.png

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

对于 get_or_create

答案是 limit 1

示例代码:

from loguru import logger
from core.mysql.models import FoundedVideoCoverImageOcrCountTable


video_id = 2147483647

fvcisc, fvcisc_created = FoundedVideoCoverImageOcrCountTable.get_or_create(
    video_id=video_id)

使用 wireshark 抓包,发现

图片.png

SELECT
    `t1`.`id`,
    `t1`.`video_id`,
    `t1`.`search_count`,
    `t1`.`created_at`,
    `t1`.`updated_at`
FROM
    `found_video_cover_image_ocr_count` AS `t1`
WHERE
    (`t1`.`video_id` = 2147483647)
LIMIT
    1 OFFSET 0

对于 get_or_none

答案和 get_or_create 是一模一样的

对于 get

答案和 get_or_create 是一模一样的


universe_king
3.4k 声望680 粉丝