fix(api-key):软删除apikey后key没有被释放后续无法再自定义相同的key#1343
Merged
Wei-Shaw merged 1 commit intoWei-Shaw:mainfrom Mar 30, 2026
Merged
Conversation
liafonx
pushed a commit
to liafonx/sub2api
that referenced
this pull request
Apr 11, 2026
…lict-after-soft-delete fix(api-key):软删除apikey后key没有被释放后续无法再自定义相同的key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#1333
修复 API Key 软删除后因唯一约束无法复用同名 key 的问题
问题概述
在 API Key 管理场景中,删除操作采用软删除(仅设置
deleted_at),但api_keys.key字段存在全局唯一约束。因此,当某个 key 被软删除后,数据库中该行仍保留原始
key值,后续再次创建同名 key 会触发唯一冲突,导致创建失败。问题原因
当前实现中:
deleted_at,不修改keykey字段声明了Unique()这意味着“历史软删除记录”仍占用唯一键空间,和“允许复用已删除 key”的业务预期冲突。
修复方案
修改位置:
internal/repository/api_key_repo.go的Delete方法。在软删除时,除了设置
deleted_at,同时将原始key改写为 tombstone 值(墓碑 key),释放原 key 的唯一约束占用。建议 tombstone 规则:
__deleted__id与高精度时间戳(如UnixNano)MaxLen(128)示例格式:
__deleted__{id}__{unix_nano}这样可确保:
修复效果
兼容性与风险说明
key值,不再保留原始明文 key。id或审计字段追踪。测试说明
在
internal/repository/api_key_repo_integration_test.go增加以下用例:TestCreate_AfterSoftDelete_AllowsSameKey运行命令