Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21566 closed Bug (fixed)

Creation of models with ForeignObject fails with bulk_create() function

Reported by: rogerhu Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The bulk_create() function uses local_fields instead of concrete_fields.

Change History (8)

comment:1 by rhu@…, 11 years ago

--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -389,7 +389,7 @@ class QuerySet(object):
             return objs
         self._for_write = True
         connection = connections[self.db]
-        fields = self.model._meta.local_fields
+        fields = self.model._meta.concrete_fields
         with transaction.commit_on_success_unless_managed(using=self.db):
             if (connection.features.can_combine_inserts_with_and_without_auto_increment_pk
                     and self.model._meta.has_auto_field):
diff --git a/tests/foreign_object/tests.py b/tests/foreign_object/tests.py
index 66f57b6..b1502c3 100644
--- a/tests/foreign_object/tests.py
+++ b/tests/foreign_object/tests.py
@@ -4,7 +4,7 @@ from operator import attrgetter
 from .models import (
     Country, Person, Group, Membership, Friendship, Article,
     ArticleTranslation, ArticleTag, ArticleIdea, NewsArticle)
-from django.test import TestCase
+from django.test import TestCase, skipUnlessDBFeature
 from django.utils.translation import activate
 from django.core.exceptions import FieldError
 from django import forms
@@ -380,6 +380,13 @@ class MultiColumnFKTests(TestCase):
                     'active_translation')[0].active_translation.title,
                 "foo")
 
+    @skipUnlessDBFeature('has_bulk_insert')
+    def test_batch_create_foreign_object(self):
+        self.bob.person_country = self.usa
+
+        objs = [Person(name=i, person_country=self.usa) for i in range(0, 5)]
+        Person.objects.bulk_create(objs, 10)
+

Last edited 11 years ago by Aymeric Augustin (previous) (diff)

comment:3 by Baptiste Mispelon, 11 years ago

Component: UncategorizedDatabase layer (models, ORM)
Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Hi,

I can reproduce the issue being reported and the provided patch does seem to fix the problem.

I left a few comments on the pull request. Once those are fixed I think this should be good to go.

Thanks.

comment:4 by rogerhu, 11 years ago

Thanks. I fixed against your comments. Also, since bulk creation is disabled for inherited models, local_concrete_fields vs. concrete_fields may not matter too much, but I've changed. Thanks again for the fast review!

comment:5 by Baptiste Mispelon <bmispelon@…>, 11 years ago

Resolution: fixed
Status: newclosed

In bbc73e6a12227f5ed52fd38bc37f56f434a0a72c:

Fixed #21566 -- Fixed AttributeError when using bulk_create with ForeignObject.

comment:6 by Tim Graham <timograham@…>, 11 years ago

In 9a446211bd13998aa6f0b6a3e6e6cdd1c69df704:

[1.6.x] Fixed #21566 -- Fixed AttributeError when using bulk_create with ForeignObject.

Backport of bbc73e6a12 from master.

comment:7 by Tim Graham <timograham@…>, 11 years ago

In 865392c47899d70229d2450ec398582f31e3cf1d:

Added 1.6.3 release note for refs #21566.

comment:8 by Tim Graham <timograham@…>, 11 years ago

In a6f05af016b5488b42b9334ec915d6dfd2702c00:

[1.6.x] Added 1.6.3 release note for refs #21566.

Backport of 865392c478 from master

Note: See TracTickets for help on using tickets.
Back to Top