欢迎光临
我们一直在努力

peewee 的 get、get_or_none、get_or_create 遇到多个值,是报错还是 limit1 ?

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

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

对于 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 是一模一样的

https://segmentfault.com/a/1190000046093860

未经允许不得转载:IT极限技术分享汇 » peewee 的 get、get_or_none、get_or_create 遇到多个值,是报错还是 limit1 ?

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址