From 927cd07a3f1098489c3860fa1ccebb35e30f66a7 Mon Sep 17 00:00:00 2001 From: "Apple\\Apple" Date: Fri, 13 Jun 2025 12:39:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(ability):=20prevent=20duplic?= =?UTF-8?q?ate=20(group,=20model)=20pairs=20when=20saving=20channels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When importing large model lists (≈700+) an attempt to save a channel could fail with: Error 1062 (23000): Duplicate entry 'default-DeepSeek-1' for key 'abilities.PRIMARY' Root cause: AddAbilities / UpdateAbilities inserted the same (group, model) pair multiple times if the input list contained duplicates or case-variants (e.g. `default` vs `Default`). Changes: • ability.go – AddAbilities: introduced `abilitySet` to deduplicate by lower-cased `group|model` key before batch-inserting. – UpdateAbilities: applied the same deduplication logic when rebuilding abilities inside a transaction. Notes: • The lower-casing is only for set comparison; the original `group` and `model` values are preserved when persisting to DB, so case sensitivity of stored data is unchanged. • Batch chunking logic (lo.Chunk) and performance characteristics remain unaffected. Fixes #1215 --- model/ability.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/model/ability.go b/model/ability.go index 38b0bd73..5c01ff7d 100644 --- a/model/ability.go +++ b/model/ability.go @@ -133,9 +133,15 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, func (channel *Channel) AddAbilities() error { models_ := strings.Split(channel.Models, ",") groups_ := strings.Split(channel.Group, ",") + abilitySet := make(map[string]struct{}) abilities := make([]Ability, 0, len(models_)) for _, model := range models_ { for _, group := range groups_ { + key := strings.ToLower(group) + "|" + strings.ToLower(model) + if _, exists := abilitySet[key]; exists { + continue + } + abilitySet[key] = struct{}{} ability := Ability{ Group: group, Model: model, @@ -194,9 +200,15 @@ func (channel *Channel) UpdateAbilities(tx *gorm.DB) error { // Then add new abilities models_ := strings.Split(channel.Models, ",") groups_ := strings.Split(channel.Group, ",") + abilitySet := make(map[string]struct{}) abilities := make([]Ability, 0, len(models_)) for _, model := range models_ { for _, group := range groups_ { + key := strings.ToLower(group) + "|" + strings.ToLower(model) + if _, exists := abilitySet[key]; exists { + continue + } + abilitySet[key] = struct{}{} ability := Ability{ Group: group, Model: model,