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'/radarr')
async def add_movie(movie_to_add: AddMovieSchema):
    tmdb_id = movie_to_add.remoteMovie.tmdbId
    folder ='/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: - "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\", 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\", line 59, in __call__      
    return await, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", 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\", line 181, in __call__
    raise exc from None
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\middleware\", line 159, in __call__
    await, receive, _send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", line 82, in __call__
    raise exc from None
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", line 71, in __call__
    await, receive, sender)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", line 580, in __call__
    await route.handle(scope, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", line 241, in handle
    await, receive, send)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\starlette\", line 52, in app
    response = await func(request)
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\", line 216, in app
    raw_response = await run_endpoint_function(
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\fastapi\", line 149, in run_endpoint_function
    return await**values)
  File ".\app\routers\api\", 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\", 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\", 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\", line 949, in get
  File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\ormar\queryset\", line 225, in check_single_result_rows_count
    raise NoMatch()

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,


Related questions

No questions were found.
Github User Rank List