django-simple-history icon indicating copy to clipboard operation
django-simple-history copied to clipboard

Documented auto_now usage with bulk_update_with_history

Open twolfson opened this issue 3 years ago • 3 comments

Description

When using bulk_update_with_history, a DateTimeField using auto_now doesn't automatically update like it does with Model.save()

This PR documents the issue under "Common Issues" and offers a convention-based solution

Related Issue

https://github.com/jazzband/django-simple-history/issues/1054

Motivation and Context

The motivation is to spread awareness of the issue and guide people towards a solution. It seems like a rather quiet bug that can really hurt data accuracy.

How Has This Been Tested?

The code is excerpts from our code

Screenshots (if appropriate):

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x] I have run the pre-commit run command to format and lint.
  • [x] My change requires a change to the documentation.
  • [x] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [x] I have added tests to cover my changes.
  • [x] I have added my name and/or github handle to AUTHORS.rst
  • [x] I have added my change to CHANGES.rst
  • [x] All new and existing tests passed.

twolfson avatar Oct 31 '22 23:10 twolfson

Codecov Report

Merging #1055 (dd38fb0) into master (6d09c3e) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1055   +/-   ##
=======================================
  Coverage   97.23%   97.23%           
=======================================
  Files          23       23           
  Lines        1231     1231           
  Branches      199      199           
=======================================
  Hits         1197     1197           
  Misses         16       16           
  Partials       18       18           

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov[bot] avatar Oct 31 '22 23:10 codecov[bot]

@twolfson what's the point? Don't you already have those auto_now dates on the history objects? I've traditionally ignored updated/created columns from my base model in my historical model

rossmechanic avatar Dec 02 '22 16:12 rossmechanic

I think there's 2 ways to interpret your response so I'll respond to both:

  • Why do you need an updated_at column on your normal model? (this is the fix I'm proposing to document)
    • This is a great question!
    • It's not used in application code so much, but it's very useful to avoid looking at extra pages or doing complex joins when looking into issues
  • Why does the historical model need an updated_at column?
    • It doesn't but this issue isn't about that. It's about the first one -- when using bulk_update_with_history, we want to automatically update the auto_now column on the normal model
    • Arguably this isn't django-simple-history responsibility, but that's why it's only being proposed as documentation, not as a code-based solution

twolfson avatar Dec 02 '22 19:12 twolfson