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

Ensure mtime update is triggered for Django #2935

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions aiida/backends/tests/orm/implementation/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ def test_creation_with_time(self):
self.assertEqual(node.ctime, ctime)
self.assertEqual(node.mtime, mtime)

def test_mtime(self):
"""Test the `mtime` is automatically updated when a database field is updated."""
node = self.node.store()
node_mtime = node.mtime

node.label = 'changed label'
self.assertTrue(node.mtime > node_mtime)

def test_clone(self):
"""Test the `clone` method."""
node = self.node.store()
Expand Down
4 changes: 4 additions & 0 deletions aiida/orm/implementation/django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def _flush(self, fields=None):
""" If the user is stored then save the current value """
if self.is_saved():
try:
# Manually append the `mtime` to fields to update, because when using the `update_fields` keyword of the
# `save` method, the `auto_now` property of `mtime` column is not triggered
if self._is_model_field('mtime'):
fields.add('mtime')
self._model.save(update_fields=fields)
except django.db.IntegrityError as e:
# Convert to one of our exceptions
Expand Down