﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24698	Relation clear fails silently when using OneToOneField+UUIDField	Julian Bez	Abhaya Agarwal	"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');
}}}
"	Bug	closed	Database layer (models, ORM)	1.8	Release blocker	fixed	uuidfield onetoonefield manytomanyfield		Accepted	1	0	0	1	0	0
