supabase-py icon indicating copy to clipboard operation
supabase-py copied to clipboard

Method `maybe_single` errors when there are no results

Open thisisthemurph opened this issue 1 year ago • 1 comments

Describe the bug

The maybe_single method seems to work when there is a single record in the database matching the query, but if there are no matching results in the database, an error is raised.

To Reproduce

An example of a query is:

result = self.supabase.table("reviews").select("*").eq("id", 31).maybe_single().execute()

If there is a record with the id of 31, the record is returned. If there is not, an exception is raised.

The exception is as below:

Traceback (most recent call last):
  File "C:\Projects\reel\venv\Lib\site-packages\postgrest\_sync\request_builder.py", line 128, in execute
    r = super().execute()
        ^^^^^^^^^^^^^^^^^
  File "C:\Projects\reel\venv\Lib\site-packages\postgrest\_sync\request_builder.py", line 117, in execute
    raise APIError(r.json())
postgrest.exceptions.APIError: {'code': 'PGRST116', 'details': 'Results contain 0 rows, application/vnd.pgrst.object+json requires 1 row', 'hint': None, 'message': 'JSON object requested, multiple (or no) rows returned'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Projects\reel\src\main.py", line 63, in <module>
    asyncio.run(main())
  File "C:\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Projects\reel\src\main.py", line 59, in main
    await scrape_movie_reviews(movies_repo, reviews_repo)
  File "C:\Projects\reel\src\main.py", line 44, in scrape_movie_reviews
    reviews_repo.add_or_update(review)
  File "C:\Projects\reel\src\repositories\reviews.py", line 37, in add_or_update
    result = self.supabase.table("reviews").select("*").eq("id", 31).maybe_single().execute()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\reel\venv\Lib\site-packages\postgrest\_sync\request_builder.py", line 131, in execute
    return SingleAPIResponse.from_dict(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\reel\venv\Lib\site-packages\postgrest\base_request_builder.py", line 191, in from_dict
    return cls(
           ^^^^
  File "pydantic\main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for SingleAPIResponse
data
  none is not an allowed value (type=type_error.none.not_allowed)

Expected behavior I expected that when there were no results, response.data would be None and no exception would be thrown.

thisisthemurph avatar Jul 31 '23 06:07 thisisthemurph

Hey,

Thanks for filing the error! This should be fixed once corresponding Postgrest-py upgrade is merged into this branch. Let us know if this is not the case.

Will leave the issue open until we bump the version and test the fix. You should be able to manually override/bump the postgrest-py version before then if needed though

J0 avatar Sep 08 '23 18:09 J0

Closing as there was no reply from OP.

silentworks avatar Apr 28 '24 22:04 silentworks