#24698: Relation clear fails silently when using OneToOneField+UUIDField
-------------------------------------+-------------------------------------
     Reporter:  webjunkie            |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.8
  (models, ORM)                      |   Keywords:  uuidfield onetoonefield
     Severity:  Normal               |  manytomanyfield
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 I have the following simplified model:

 {{{
 class Lead(models.Model):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
 editable=False)
     name = models.CharField(max_length=255)


 class LeadInfo(models.Model):
     lead = models.OneToOneField(Lead, primary_key=True)
     url = models.URLField("Blog URL", blank=True)
     topics = models.ManyToManyField("Topic", blank=True)


 class Topic(models.Model):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
 editable=False)
     name = models.CharField(max_length=255)

 }}}

 There seems to be a problem with the clear() on the ManyToManyField.
 Django tries to delete entries, but uses the uuid with hyphens, although
 it previously saved the uuid without. The clear does nothing, and the next
 add will then fail. (This becomes a problem in ModelForms, as they clear
 relations before updating). This is on MySQL.


 {{{
 >>> l = Lead.objects.create(name="Lead 1")
 >>> topic = Topic.objects.create(name="Topic 1")
 >>> info = LeadInfo.objects.create(lead=l, url="bla")
 >>> info.topics.add(topic)
 >>> info.topics.clear()
 >>> info.topics.add(topic)
 IntegrityError: (1062, "Duplicate entry
 '5357d002c2d74e1899b845a596e10cd8-15572c1b20234a6caa917cb200a2436' for key
 'leadinfo_id'")
 }}}

 I've traced it down to Django making a wrong query for the delete, but I
 cannot tell why the hyphens end up in there, whereas in the INSERT it does
 them right:

 {{{
 DELETE FROM `uuidtest_leadinfo_topics` WHERE
 `uuidtest_leadinfo_topics`.`leadinfo_id` =
 '5357d002-c2d7-4e18-99b8-45a596e10cd8'";
 INSERT INTO `uuidtest_leadinfo_topics` (`leadinfo_id`, `topic_id`) VALUES
 ('5357d002c2d74e1899b845a596e10cd8', '15572c1b20234a6caa917cb200a2436b');
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24698>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.2683b8f076a8174b9ac5eebe9061d7a1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to