Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`table.get(column=value)` option for retrieving things not by their primary key

未关闭 适合新手
#588 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
冷清
技术栈
python, sqlite
领域
databases

调研方向

从 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:

https://github.com/simonw/sqlite-utils/blob/1260bdc7bfe31c36c272572c6389125f8de6ef71/sqlite_utils/db.py#L1495

主要语言
Python
星标
2.2k
派生
172
平均合并
9 分钟
30 天内合并 PR
1

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

simonw/sqlite-utils 的其他 Issue

查看 simonw/sqlite-utils 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。