google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

Add a convenience method to fetch data for a single row

Open tseaver opened this issue 9 years ago • 6 comments

@jonparrott would prefer:

row = database.read_row(table='Albums', columns=('MarketingBudget',), key=[1, 1]))

tseaver avatar Feb 15 '17 17:02 tseaver

@jonparrott read takes a keyset and returns a sequence of row tuples. If we implement read_rows as a thin wrapper around read, what semantics should we have for a read which returns no rows? Raise a NotFound? Return None?

tseaver avatar Jul 25 '17 20:07 tseaver

I'm leaning towards NotFound (like datastore), but @vkedia should weigh in.

theacodes avatar Jul 25 '17 20:07 theacodes

It would be great for query iterators also to have a get_one or one method that returns the single row yielded by the query, else throwing an exception.

Note that SQLAlchemy has a similar one method: http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.one

mcurtiss avatar Aug 07 '17 01:08 mcurtiss

Regarding https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3021#issuecomment-317859229 it is definitely valid for the read to not return any row at all. User might just be checking for the existence of row. So None seems better.

Regarding https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3021#issuecomment-320546633 I agree it would be useful to have this and some of the other methods like one_or_none, first. Is there no common Python library that exposes these type of methods for an arbitrary iterator. Similar to this in Java.

vkedia avatar Aug 07 '17 18:08 vkedia

Regarding https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3021#issuecomment-317859229 it is definitely valid for the read to not return any row at all. User might just be checking for the existence of row. So None seems better.

~I would prefer empty list over None, I think.~ I would prefer an exception actually, since that is what other Python ORMs do.

Regarding https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3021#issuecomment-320546633 I agree it would be useful to have this and some of the other methods like one_or_none, first. Is there no common Python library that exposes these type of methods for an arbitrary iterator. Similar to this in Java.

I am not aware of any such animal (nor have I ever wanted one).

lukesneeringer avatar Aug 08 '17 06:08 lukesneeringer

What semantics would we want for a read that returns more than one row? We could have it send limit=1 to read, but both SQLAlchemy and Django raise exceptions in this case.

lukesneeringer avatar Aug 10 '17 15:08 lukesneeringer