#21612: queryset update ignores to_field on foreign keys
-------------------------------------+-------------------------------------
     Reporter:  berndtj@…            |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.5
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by berndtj@…):

 I was gone during the holidays.

 I can verify that this behavior exists on the 1.6 branch:

 In [23]: b
 Out[23]: <BracketUser: n...@berndt.com>

 In [25]: BracketGroup.objects.filter(created_by='ber...@berndt.com')
 Out[25]: [<BracketGroup [made by old] ID: 18b72e3766d947b29f802b637785caf8
 - created by ber...@berndt.com>, <BracketGroup [default] ID:
 96c6018e9ebc4c69a96636bab4ee9aaf - created by ber...@berndt.com>]

 In [26]: qs = BracketGroup.objects.filter(created_by='ber...@berndt.com')

 In [27]: qs.update(created_by=b)
 ---------------------------------------------------------------------------
 IntegrityError                            Traceback (most recent call
 last)
 /home/vagrant/work/django/<ipython-input-27-1536333ef175> in <module>()
 ----> 1 qs.update(created_by=b)

 /home/vagrant/work/django/django/db/models/query.pyc in update(self,
 **kwargs)
     488         query.add_update_values(kwargs)
     489         with
 transaction.commit_on_success_unless_managed(using=self.db):
 --> 490             rows = query.get_compiler(self.db).execute_sql(None)
     491         self._result_cache = None
     492         return rows

 /home/vagrant/work/django/django/db/transaction.pyc in __exit__(self,
 exc_type, exc_value, traceback)
     303                     # Commit transaction

     304                     try:
 --> 305                         connection.commit()
     306                     except DatabaseError:
     307                         connection.rollback()

 /home/vagrant/work/django/django/db/backends/__init__.pyc in commit(self)
     166         self.validate_thread_sharing()
     167         self.validate_no_atomic_block()
 --> 168         self._commit()
     169         self.set_clean()
     170

 /home/vagrant/work/django/django/db/backends/__init__.pyc in _commit(self)
     134         if self.connection is not None:
     135             with self.wrap_database_errors:
 --> 136                 return self.connection.commit()
     137
     138     def _rollback(self):

 /home/vagrant/work/django/django/db/utils.pyc in __exit__(self, exc_type,
 exc_value, traceback)
      97                 if dj_exc_type not in (DataError, IntegrityError):
      98                     self.wrapper.errors_occurred = True
 ---> 99                 six.reraise(dj_exc_type, dj_exc_value, traceback)
     100
     101     def __call__(self, func):

 /home/vagrant/work/django/django/db/backends/__init__.pyc in _commit(self)
     134         if self.connection is not None:
     135             with self.wrap_database_errors:
 --> 136                 return self.connection.commit()
     137
     138     def _rollback(self):

 IntegrityError: insert or update on table "nomos_bracketgroup" violates
 foreign key constraint "created_by_id_refs_username_32f63683ba6621ac"
 DETAIL:  Key (created_by_id)=(8) is not present in table
 "nomos_bracketuser".


 In [28]: b.pk
 Out[28]: 8

 It's a bit of a different example, but the issue is the same.  As you can
 see, django is trying to update the created_by_id with the PK instead of
 the to_field which in this case is the username attribute of
 BracketUser... so it fails.

 Testing with master is a bit more work, as things don't appear to just
 work...

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21612#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/075.e9d8e9389054deeaa9d838798d855259%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to