Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

update_or_create raising NoMatch()

See original GitHub issue

Issue Description

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()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

collerekcommented, Jul 19, 2021

@pawamoy - yeah I need to update the docs -> will include this in the next release.

And exactly it has to work this way as otherwise all kwargs would be used to select an object

  • so there is nothing to update (if one is found using all kwargs it’s already the same)
  • or would result in an integrity error if it’s not found but the pk is duplicated (so instead of update would try to create a new one with the same pk).

So it’s actually using all kwargs without the primary key to update the object and determines create/update based on the primary key presence.

muqshotscommented, Jul 13, 2021

Alright thanks!, I’ll close the issue now, Perhaps the documentation can be updated to clarify this a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel updateOrCreate with auto-incremental database
My purpose is to update if the value exists, else inserts a new row in the database table after submitting the form. The...
Read more >
Update data in database - ormar
update_or_create. update_or_create(**kwargs) -> Model. Updates the model, or in case there is no match in database creates a new one.
Read more >
WO2006014439A2 - Hotspot location record database
The information may be extracted by the database to update or create records ... First, they enable scaling with increasing volumes of directory...
Read more >
ex_aws_s3/s3.ex at main
raise "not yet implemented". request(:put, bucket, "/"). end. @doc "Update or create a bucket notification configuration".
Read more >
GNU gettext utilities
The argument must be non-NULL and have been created through create_foo(). ... Any translation work done seriously will raise many linguistic difficulties, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found