Skip to content

Commit 468262a

Browse files
committed
Add test for atomic update with F of a field with a different db_column
1 parent 23b9bcc commit 468262a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

tests/query/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Person(models.Model):
2525
name = models.CharField(max_length=20)
2626
surname = models.CharField(max_length=20)
2727
age = models.IntegerField(null=True, blank=True)
28+
another_age = models.IntegerField(null=True, blank=True, db_column='age2')
2829

2930
class Meta:
3031
unique_together = ("name", "surname")

tests/query/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,17 @@ def test_update_with_F(self):
375375
Person.objects.filter(name='john').update(age=F('age')-10)
376376
self.assertEqual(Person.objects.get(name='john').age, 39)
377377

378+
def test_update_with_F_and_db_column(self):
379+
# This test is simmilar to test_update_with_F but tests
380+
# the update with a column that has a db_column set.
381+
john = Person.objects.create(name='john', surname='nhoj', another_age=42)
382+
andy = Person.objects.create(name='andy', surname='ydna', another_age=-5)
383+
Person.objects.update(another_age=F('another_age')+7)
384+
self.assertEqual(Person.objects.get(pk=john.id).another_age, 49)
385+
self.assertEqual(Person.objects.get(id=andy.pk).another_age, 2)
386+
Person.objects.filter(name='john').update(another_age=F('another_age')-10)
387+
self.assertEqual(Person.objects.get(name='john').another_age, 39)
388+
378389
def test_invalid_update_with_F(self):
379390
self.assertRaises(AssertionError, Person.objects.update, age=F('name')+1)
380391

0 commit comments

Comments
 (0)