Allow table.get() to look up rows by column values - #884
Open
vishalkaay wants to merge 1 commit into
Open
vishalkaay wants to merge 1 commit into
vishalkaay wants to merge 1 commit into
Conversation
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
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.
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 acollectionstable keyed byidwith a uniquename), fetching by that column means falling back torows_where()boilerplate.This lets you pass one or more
column=valuekeyword arguments instead:The first matching row is returned, and
last_pkis set to that row's primary key so the result can be chained intoupdate()/delete()just like a normalget(). Multiple keyword arguments are ANDed together. Passing both a primary key and keyword arguments raisesValueError, and callingget()with neither raisesTypeError. 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/