-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
table.pks_and_rows_where() method returning primary keys along with the rows #240
Comments
One option: Or... how about you can call I'm always a little bit nervous of methods that vary their return type based on their arguments. Maybe this would be a separate method instead? for pk, row in table.pk_rows_where(...):
# ... |
Is Other options:
|
I quite like |
|
I prototyped this and I like it:
|
Example from the docs: >>> db = sqlite_utils.Database(memory=True)
>>> db["dogs"].insert({"name": "Cleo"})
>>> for pk, row in db["dogs"].pks_and_rows_where():
... print(pk, row)
1 {'rowid': 1, 'name': 'Cleo'}
>>> db["dogs_with_pk"].insert({"id": 5, "name": "Cleo"}, pk="id")
>>> for pk, row in db["dogs_with_pk"].pks_and_rows_where():
... print(pk, row)
5 {'id': 5, 'name': 'Cleo'}
>>> db["dogs_with_compound_pk"].insert(
... {"species": "dog", "id": 3, "name": "Cleo"},
... pk=("species", "id")
... )
>>> for pk, row in db["dogs_with_compound_pk"].pks_and_rows_where():
... print(pk, row)
('dog', 3) {'species': 'dog', 'id': 3, 'name': 'Cleo'} |
Original title: Easier way to update a row returned from .rows
Here's a surprisingly hard problem I ran into while trying to implement #239 - given a row returned by
db[table].rows
how can you update that row?The problem is that the
db[table].update(...)
method requires a primary key. But if you have a row from thedb[table].rows
iterator it might not even contain the primary key - provided the table is arowid
table.Instead, currently, you need to introspect the table and, if
rowid
is a primary key, explicitly include that in theselect=
argument totable.rows_where(...)
- otherwise it will not be returned.A utility mechanism to make this easier would be very welcome.
The text was updated successfully, but these errors were encountered: