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 没有添加唯一约束,且存在多个一样的值
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 抓包,发现
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 是一模一样的
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。