`table.get(column=value)` option for retrieving things not by their primary key
还没有人认领这个 Issue。
评估
调研方向
从 sqlite_utils/db.py 大约第 1495 行开始,查看现有 Table.get() 方法的定义,并检查其当前按位置查找的行为。添加按关键字进行列查找的支持,使其匹配 issue 中的 unique-name 示例,同时保留 NotFoundError 的行为;然后运行现有的数据库测试,并为查找成功和行不存在的情况添加覆盖。
由索引模型根据 Issue 内容生成。
描述
This came up working on this feature:
I have a table with this schema:
CREATE TABLE [collections] (
[id] INTEGER PRIMARY KEY,
[name] TEXT,
[model] TEXT
);
CREATE UNIQUE INDEX [idx_collections_name]
ON [collections] ([name]);
So the primary key is an integer (because it's going to have a huge number of rows foreign key related to it, and I don't want to store a larger text value thousands of times), but there is a unique constraint on the name - that would be the primary key column if not for all of those foreign keys.
Problem is, fetching the collection by name is actually pretty inconvenient.
Fetch by numeric ID:
try:
table["collections"].get(1)
except NotFoundError:
# It doesn't exist
Fetching by name:
def get_collection(db, collection):
rows = db["collections"].rows_where("name = ?", [collection])
try:
return next(rows)
except StopIteration:
raise NotFoundError("Collection not found: {}".format(collection))
It would be neat if, for columns where we know that we should always get 0 or one result, we could do this instead:
try:
collection = table["collections"].get(name="entries")
except NotFoundError:
# It doesn't exist
The existing .get() method doesn't have any non-positional arguments, so using **kwargs like that should work:
- 主要语言
- Python
- 星标
- 2.2k
- 派生
- 172
- 平均合并
- 9 分钟
- 30 天内合并 PR
- 1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
simonw/sqlite-utils 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
simonw/sqlite-utils#876 ·
-
难度 2/5 1-3 小时 新手友好度 90/100
simonw/sqlite-utils#872 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
simonw/sqlite-utils#857 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
simonw/sqlite-utils#849 ·
-
bug documentation help wanted
难度 1/5 1 小时以内 新手友好度 88/100
simonw/sqlite-utils#493 · 5 条评论 ·
查看 simonw/sqlite-utils 的全部 Issue
相似的 Issue
-
agent-ready documentation needs-triage
难度 1/5 1-3 小时 新手友好度 88/100
-
documentation
难度 1/5 1 小时以内 新手友好度 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
-
instance instance add
难度 1/5 1 小时以内 新手友好度 72/100
searxng/searx-instances#939 · 1 条评论 ·
-
area-deployment area-integrations triage:bot-seen
难度 2/5 半天 新手友好度 86/100