Skip to content
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

Method maybe_single errors when there are no results #511

Closed
thisisthemurph opened this issue Jul 31, 2023 · 2 comments
Closed

Method maybe_single errors when there are no results #511

thisisthemurph opened this issue Jul 31, 2023 · 2 comments

Comments

@thisisthemurph
Copy link

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.

@J0
Copy link
Collaborator

J0 commented Sep 8, 2023

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

@silentworks
Copy link
Contributor

Closing as there was no reply from OP.

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

No branches or pull requests

3 participants