profile
viewpoint

Ask questionsupdate_or_create raising NoMatch()

Describe the bug method update_or_create raises NoMatch() instead of creating object

To Reproduce Steps to reproduce the behavior: Models defined as follows

class Movie(ormar.Model):
    class Meta(BaseMeta):
        tablename = 'movies'

    tmdb_id: int = ormar.Integer(nullable=False, primary_key=True)
    path: str = ormar.String(max_length=255)

route as follows

@router.post('/radarr')
async def add_movie(movie_to_add: AddMovieSchema):
    tmdb_id = movie_to_add.remoteMovie.tmdbId
    folder = movie_to_add.movie.folderPath.lstrip('/data/media/movies/merge/')
    filename = movie_to_add.movieFile.relativePath
    moviepath = os.path.join(folder, filename)
    movie = await Movie.objects.update_or_create(tmdb_id=tmdb_id, path=moviepath)
    return {}

(Note: this should be a complete and concise piece of code that allows reproduction of an issue)

Expected behavior If the pk tmdb_id exists, it will update the path column If the pk tmdb_id does not exist it will create the row

Screenshots If applicable, add screenshots to help explain your problem.

Versions (please complete the following information):

  • Database backend used (mysql/sqlite/postgress) - sqlite
  • Python version - 3.9.1
  • ormar version - 0.10.14
  • pydantic version - 1.8.2
  • if applicable fastapi version - 0.66.0

Additional context error traceback

INFO:     127.0.0.1:63368 - "POST /radarr HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 371, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 59, in __call__      
    return await self.app(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc from None
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc from None
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\routing.py", line 52, in app
    response = await func(request)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\routing.py", line 216, in app
    raw_response = await run_endpoint_function(
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\routing.py", line 149, in run_endpoint_function
    return await dependant.call(**values)
  File ".\app\routers\api\mediarr.py", line 20, in add_movie
    movie = await Movie.objects.update_or_create(tmdb_id=tmdb_id, path=path)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\ormar\queryset\queryset.py", line 987, in update_or_create
    model = await self.get(pk=kwargs[pk_name])
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\ormar\queryset\queryset.py", line 929, in get
    return await self.filter(*args, **kwargs).get()
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\ormar\queryset\queryset.py", line 949, in get
    self.check_single_result_rows_count(processed_rows)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\ormar\queryset\queryset.py", line 225, in check_single_result_rows_count
    raise NoMatch()
ormar.exceptions.NoMatch
collerek/ormar

Answer questions pawamoy

Bumped into this as well. I was wondering anyway how ormar could know which kwargs to use to get the row and which one to use to update it / create it. Now I know that it always tries to create without an id/pk,

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List