Skip to content

Commit

Permalink
Fixed #29974 -- Fixed non-truthy primary key values for QuerySet.bulk…
Browse files Browse the repository at this point in the history
…_update().
  • Loading branch information
orf authored and timgraham committed Nov 22, 2018
1 parent 5a71bd7 commit 3140844
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def bulk_update(self, objs, fields, batch_size=None):
if not fields:
raise ValueError('Field names must be given to bulk_update().')
objs = tuple(objs)
if not all(obj.pk for obj in objs):
if any(obj.pk is None for obj in objs):
raise ValueError('All bulk_update() objects must have a primary key set.')
fields = [self.model._meta.get_field(name) for name in fields]
if any(not f.concrete or f.many_to_many for f in fields):
Expand Down
1 change: 1 addition & 0 deletions tests/queries/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ class MyObject(models.Model):

class Order(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=12, null=True, default='')

class Meta:
ordering = ('pk',)
Expand Down
9 changes: 8 additions & 1 deletion tests/queries/test_bulk_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .models import (
Article, CustomDbColumn, CustomPk, Detail, Individual, Member, Note,
Number, Paragraph, SpecialCategory, Tag, Valid,
Number, Order, Paragraph, SpecialCategory, Tag, Valid,
)


Expand Down Expand Up @@ -167,6 +167,13 @@ def test_custom_pk(self):
[cat.extra for cat in custom_pks]
)

def test_falsey_pk_value(self):
order = Order.objects.create(pk=0, name='test')
order.name = 'updated'
Order.objects.bulk_update([order], ['name'])
order.refresh_from_db()
self.assertEqual(order.name, 'updated')

def test_inherited_fields(self):
special_categories = [
SpecialCategory.objects.create(name=str(i), special_name=str(i))
Expand Down

0 comments on commit 3140844

Please sign in to comment.