nnyby nnyby - 1 year ago 129
Python Question

Django: one-to-one field with factory_boy: UNIQUE constraint failed

I'm trying to make factories for these models with factory_boy:

class Course(models.Model):
group = models.OneToOneField(Group)
title = models.CharField(max_length=1024)

class CourseInfo(models.Model):
course = models.OneToOneField(Course, related_name='info')
starttime = models.TimeField(null=True, blank=True)
endtime = models.TimeField(null=True, blank=True)

Here are my factories:

class CourseInfoFactory(factory.DjangoModelFactory):
class Meta:
model = CourseInfo

course = factory.SubFactory('courseaffils.tests.factories.CourseFactory')
starttime = time(hour=4)
endtime = time(hour=6)

class CourseFactory(factory.DjangoModelFactory):
class Meta:
model = Course

title = factory.Sequence(lambda n: 'Example Course %d' % n)
group = factory.SubFactory(GroupFactory)
info = factory.RelatedFactory(CourseInfoFactory, 'course')

Now, when I call
I get an IntegrityError:

IntegrityError: UNIQUE constraint failed: courseaffils_courseinfo.course_id

What's wrong with my factories? How can I fix this problem?

If I instead do
course = factory.RelatedFactory('courseaffils.tests.factories.CourseFactory')
on the CourseInfoFactory, I get this error, as expected:

IntegrityError: NOT NULL constraint failed: courseaffils_courseinfo.course_id

If you need more details, here's the actual code I'm working with:


Answer Source

The problem was that CourseFactory() already creates a CourseInfo object. I've resolved this by just removing the info = factory.RelatedFactory(CourseInfoFactory, 'course') line, and now the CourseInfoFactory is unnecessary - I can test CourseInfo by getting the created object at course.info.