Skip to content

Allow table.get() to look up rows by column values - #884

Open
vishalkaay wants to merge 1 commit into
simonw:mainfrom
vishalkaay:get-by-column
Open

vishalkaay wants to merge 1 commit into
simonw:mainfrom
vishalkaay:get-by-column

Conversation

@vishalkaay

@vishalkaay vishalkaay commented Sep 25, 2026 •

Copy link
Copy Markdown

Closes #588.

table.get() currently only accepts a primary key. When a table has an integer primary key but a separate column with a unique constraint (the example from the issue is a collections table keyed by id with a unique name), fetching by that column means falling back to rows_where() boilerplate.

This lets you pass one or more column=value keyword arguments instead:

db["collections"].get(name="entries")

The first matching row is returned, and last_pk is set to that row's primary key so the result can be chained into update()/delete() just like a normal get(). Multiple keyword arguments are ANDed together. Passing both a primary key and keyword arguments raises ValueError, and calling get() with neither raises TypeError. Existing primary-key behaviour is unchanged.

Includes tests (single column, multiple columns, compound-pk and rowid tables, not-found, and the two argument-validation errors) and a new docs section under "Retrieving a specific record".


📚 Documentation preview 📚: https://sqlite-utils--884.org.readthedocs.build/en/884/

table.get() now accepts column=value keyword arguments and returns the
first matching row, so a row can be fetched by a unique column rather
than only by its primary key. Passing both a primary key and keyword
arguments raises ValueError, and calling get() with neither raises
TypeError.

Refs simonw#588
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant