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

auto_now not working #1574

Closed
lichen404 opened this issue Mar 23, 2024 · 2 comments
Closed

auto_now not working #1574

lichen404 opened this issue Mar 23, 2024 · 2 comments

Comments

@lichen404
Copy link

Describe the bug
I use auto_now in my model,but when I use update method ,its updated time doesn't update

update mtheod

image

model

image
image

Expected behavior
auto_now works when I update data

@daudln
Copy link

daudln commented Apr 2, 2024

auto_now fields are not updated on calling .update() method.

auto_now fields are updated only if .save() method is called on model instance.

Be aware that update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save()

Based on your scenario, you need to get item, set attribute you want to update and save the instance

@app.put("/movies/{pk}")
async def update_movie(pk: int, movie_in: MovieIn):
    movie = await Movie.filter(id=pk).first()
    if movie:
        # Your staffs
        await movie.save()
        return  # Your staffs
    return  # Your staffs

@waketzheng
Copy link
Contributor

A few months ago, both auto_now and auto_now_add not work at my code. But after I change database from sqlite3 to MySQL, they work fine.

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