`table.get(column=value)` option for retrieving things not by their primary key
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start in sqlite_utils/db.py around line 1495, where the existing Table.get() method is defined, and review its current positional lookup behavior. Add support for keyword column lookup matching the issue's unique-name example, preserving NotFoundError behavior, then run the existing database tests and add coverage for a successful lookup and a missing row.
Written by the indexing model from the issue text.
Description
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:
- Dominant language
- Python
- Stars
- 2.2k
- Forks
- 172
- Avg merge
- 9m
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from simonw/sqlite-utils
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
simonw/sqlite-utils#876 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
simonw/sqlite-utils#857 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
simonw/sqlite-utils#849 ·
-
bug documentation help wanted
Difficulty 1/5 Under an hour Newbie friendliness 88/100
simonw/sqlite-utils#493 · 5 comments ·
All issues in simonw/sqlite-utils
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
stephrobert/dsoxlab#238 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
sublimehq/package_control#1780 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
nwg-piotr/nwg-displays#145 ·